المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Insert function into event at runtime



C# Programming
08-19-2009, 10:52 PM
Hi,
If i have a lot of function there is an easy way to insert them by runtime and not by code?

For example instead of this:

public class A
{

MyEvent m_Event;
List m_EventList;

public bool Func_1(int num) { MessageBox.Show("1"); return num > 1 ? true : false; }
public bool Func_2(int num) { MessageBox.Show("2"); return num > 2 ? true : false; }
public bool Func_3(int num) { MessageBox.Show("3"); return num > 3 ? true : false; }
public bool Func_4(int num) { return num > 4 ? true : false; }
public bool Func_5(int num) { return num > 5 ? true : false; }
public bool Func_6(int num) { return num > 6 ? true : false; }
public bool Func_7(int num) { return num > 7 ? true : false; }
public bool Func_8(int num) { return num > 8 ? true : false; }

public A()
{

this.m_Event += new MyEvent(Func_1);
this.m_Event += new MyEvent(Func_2);
this.m_Event += new MyEvent(Func_3);
this.m_Event += new MyEvent(Func_4);
this.m_Event += new MyEvent(Func_5);
this.m_Event += new MyEvent(Func_6);
this.m_Event += new MyEvent(Func_7);
this.m_Event += new MyEvent(Func_8);
}
}



I want this code to look like this:


public class A
{

MyEvent m_Event;
List<MyEvent> m_EventList;

public bool Func_1(int num) { MessageBox.Show("1"); return num > 1 ? true : false; }
public bool Func_2(int num) { MessageBox.Show("2"); return num > 2 ? true : false; }
public bool Func_3(int num) { MessageBox.Show("3"); return num > 3 ? true : false; }
public bool Func_4(int num) { return num > 4 ? true : false; }
public bool Func_5(int num) { return num > 5 ? true : false; }
public bool Func_6(int num) { return num > 6 ? true : false; }
public bool Func_7(int num) { return num > 7 ? true : false; }
public bool Func_8(int num) { return num > 8 ? true : false; }

public A()
{

for(int i=1;i