2011-11-17 87 views
1

我有一个类的方法将传递一个字符串。该方法将对该字符串执行一些操作,然后将该字符串传递给某个可以对该字符串执行其他操作的对象。委托或反思?

因此,它基本上是这样的:

class Main 
{ 
    public Main() 
    { 
     strClass str = new strClass(this); 
    } 

    public function handler () 
    { 
     console.log("No string is passed yet, but this method is called from receiveData()"); 
    } 
} 

class strClass 
{ 
    object handler; 
    public strClass (handler) 
    { 
     // save the object 
     this.handler = handler; 
    } 

    public receiveData (string str) 
    { 
     // This method does some stuff with the string 
     // And it then passes it on to the supplied object (handler) which will do 
     // the rest of the processing 

     // I'm calling the "handler" method in the object which got passed in the 
     // constructor 
     Type thisType = this.handler.GetType(); 
     MethodInfo theMethod = thisType.GetMethod("handler"); 
     theMethod.Invoke(this.handler, null); 
    } 
} 

现在这个代码的工作好,用反射的东西。但我想知道,这不应该是可能的(甚至更好?)与代表?如果是这样,我怎么可以通过使用委托实现这一点呢?

+3

编译器错误,使它一点点努力,以确保我们在这里回答同样的问题.. 。它会很好,如果它运行(因为它“运作良好”) –

回答

4

委托是一个更好的选择。

class Main 
{ 

    public Main() 
    { 
     StrClass str = new StrClass(this.Handler); 
    } 

    public void Handler () 
    { 
     //called from recieve data 
    } 
} 

class StrClass 
{ 
    readonly Action _handler; 
    public StrClass (Action callback) 
    { 
     // save the object 
     this._handler = callback; 
    } 

    public void receiveData(string str) 
    { 
     this._handler(); 
    } 
} 
+0

谢谢,这工作很好,似乎是一个更好的解决方案,然后我有什么。 – w00

5

你不能使用接口来代替:

interface IStringHandler { 
    void HandleString(string s); 
} 


class strClass 
{ 
     IStringHandler handler = null; 

     public strClass(IStringHandler handler) 
     { 
      this.handler = handler; 
     } 

     public void ReceiveData(string s) 
     { 
      handler.HandleString(s); 
     } 
} 


class Main : IStringHandler 
{ 
     // Your code 
} 
0

首先,如果你必须按名称调用一个未知的方法,使用dynamic - 这在很大程度上为这种优化的(虽然仍然不是一个好主意):

((dynamic)handler).handler(); // but please don't use this! see below 

但是,我反而看或者是Action<string>(或者可能是Func<string,string>),或者是其上具有已知方法的接口。

2

您可以用Action这样做:

class Main 
{ 
    public Main()  
    { 
     strClass str = new strClass(newString => 
     { 
      console.log("This string I got back: " + newString);  
     }); 
    } 
} 
class strClass 
{  
    Action<string> callback; 
    public strClass (Action<string> callback) 
    { 
     // save the action 
     this.callback = callback;  
    } 
    public receiveData (string str)  
    { 
     // Do something with the string 
     callback(str); 
    } 
} 
0

基本上,你想改变你如何对象StrClass反应,开始接收数据。听起来像事件给我。

这样的事情,那就是你有两个在Main,并且在一般HandlerObject处理方法:

class StrClass : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged = null; 
    public void OnPropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, e); 
    } 

    private string receivedString; 
    public string ReceivedString 
    { 
     get; 
     set 
     { 
      string oldStr = receivedString; 
      receivedString = value; 
      PropertyChanged(receivedString, new PropertyChangedEventArgs("ReceivedString")); 
     } 
    } 

    public void receiveData(string str) 
    { 
     //event fires here 
     ReceivedString = str; 
    } 
} 

class HandlerObject 
{ 
    public void HandlerMethod1(string s) 
    { 
     //magic 
    } 

    public void HandlerMethod2(string s) 
    { 
     //different kind of magic 
    } 
} 

class Program 
{ 
    static void HandlerMethod3(string s) 
    { 
     //another kind of magic! 
    } 

    static void Main(string[] args) 
    { 
     StrClass class1 = new StrClass(); 
     StrClass class2 = new StrClass(); 
     StrClass class3 = new StrClass(); 

     HandlerObject handler = new HandlerObject(); 

     class1.PropertyChanged += (s, e) => { handler.HandlerMethod1(s.ToString()); }; 
     class2.PropertyChanged += (s, e) => { handler.HandlerMethod2(s.ToString()); }; 
     class3.PropertyChanged += (s, e) => { HandlerMethod3(s.ToString()); }; 
    } 
}