문자열을 전달할 메서드가있는 클래스가 있습니다. 이 메소드는 문자열에 대해 몇 가지 작업을 수행 한 다음 문자열을 다른 객체를 수행 할 수있는 특정 객체에 전달합니다.대의원 또는 반성?
는 그래서 기본적으로 다음과 같습니다
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);
}
}
지금이 코드는 반사 물건, 좋은 작품
. 하지만 궁금 해서요, 가능하지 않아야합니까 (그리고 어쩌면 더 좋을까요?) 그렇다면 어떻게 대리자를 대신 사용하여 구현할 수 있습니까?
을 .. 그것은 좋은 ("좋은 작품"이후) 도망 가면 좋을거야 –