troubles with reflection

How to use method as parameter in InvokeMember? I mean

without reflection

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();

Super is a delegate.

with reflection I want to do the same thing

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,)

But I don't know what to send as parameter. Create instance of Super object?

Setplus is a property

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

obj - object of class Event

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

Upvotes: 0

Views: 102

Answers (1)

Mike Zboray
Mike Zboray

Reputation: 40798

The equivalent of:

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

Is this:

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);

Upvotes: 1

Related Questions