2012-05-06 13 views
0

InvokeMemberのmethod as parameterの使い方は? 私はスーパーがデリゲートである反射の問題

ContainerEvents ems = new ContainerEvents(); 
Test ob1 = new Test(4); 
Exam ob2 = new Exam(1); 
FExam ob3 = new FExam(1); 
Check ob4 = new Check(5); 
ems.Setplus = new Super(ob1.Write); 
ems.Setplus = new Super(ob2.Write); 
ems.Setplus = new Super(ob3.Write); 
ems.Setplus = new Super(ob4.Write); 
ems.Run(); 

反射せずに

を意味します。反射と

私は同じこと

Type type1 = typeof (Test); 
Type type2 = typeof (Exam); 
Type type3 = typeof (FExam); 
Type type4 = typeof (Check); 
Type events = typeof (ContainerEvents); 
object eventer = Activator.CreateInstance(events); 
events.InvokeMember("Setplus",BindingFlags.InvokeMethod,null,eventer,) 

をしたいしかし、私は、パラメータとして送信するのか分かりません。 Superオブジェクトのインスタンスを作成しますか?

Setplus

public Super Setplus 
{ 
    set { obj.activate += value; } 
} 

OBJ財産である - クラスイベントの対象

public class Event 
{ 
    public event Super activate ; 
    public void act() 
    { 
     if (activate != null) activate(); 
    } 
} 
+0

なぜinvokeメンバーを使用しますか?元のコードでは、Setplusにいくつかのセットを入れた後で 'Run()'を呼び出すだけです。あなたのコードはあまり意味がありません。 – SimpleVar

+0

あなたが何をしようとしているのか不明なので、あなたを助けるのが難しいです。論理名を持つようにコードを編集し、慣れ親しんでいる例を使用してください。 – SimpleVar

+2

(Stack Overflowでコードを表示するときにタブの代わりにスペースを使用してください - Markdownは実際にはタブをあまり好きではありません...) –

答えて

1

は相当:

ContainerEvents ems = new ContainerEvents(); 
Test ob1 = new Test(4); 
ems.Setplus = new Super(ob1.Write); 

はこれです:

object ems = Activator.CreateInstance(typeof(ContainerEvents)); 
object ob1 = Activator.CreateInstance(typeof(Test), new object[] { 4 }); 
object super = Delegate.CreateDelegate(typeof(Super), ob1, "Write"); 
ems.GetType().GetProperty("SetPlus").SetValue(ems, super, null);