Insert function into event at runtime
-
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<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() { 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<=8;i++) this.m\_Event += new MyEvent(Func\_ + i.ToString()); } }
is there some way to do something like this?
-
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<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() { 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<=8;i++) this.m\_Event += new MyEvent(Func\_ + i.ToString()); } }
is there some way to do something like this?
Yes. You can use System.Reflection and Delegate.CreateDelegate to help. The basic sequence of events is:
-
Get the class' Type
-
Get the EventInfo representing m_Event of that Type
-
Iterate from one to eight
-
Get the method "Func_" + i.ToString()
-
Use Delegate.CreateDelegate(typeof(MyEvent), [MethodInfo you just retrieved in the last step])
-
Store the return value of that function call in a variable
-
Call the AddEventHandler(this, [Delegate you just created and stored in a variable])
That's the basic sequence of events. If you wanted something simpler, but less modular, you could just create a List<MyEvent> and store the functions in there. Then just iterate and add the handler like you normally do.
Between the motion And the act Falls the Shadow
-
-
Yes. You can use System.Reflection and Delegate.CreateDelegate to help. The basic sequence of events is:
-
Get the class' Type
-
Get the EventInfo representing m_Event of that Type
-
Iterate from one to eight
-
Get the method "Func_" + i.ToString()
-
Use Delegate.CreateDelegate(typeof(MyEvent), [MethodInfo you just retrieved in the last step])
-
Store the return value of that function call in a variable
-
Call the AddEventHandler(this, [Delegate you just created and stored in a variable])
That's the basic sequence of events. If you wanted something simpler, but less modular, you could just create a List<MyEvent> and store the functions in there. Then just iterate and add the handler like you normally do.
Between the motion And the act Falls the Shadow
Thanks for your help , it's works :-\
-
-
Thanks for your help , it's works :-\
Cool. I'm glad that you found a solution! I would have used a (yuck) macro. But, from what I could find, C# doesn't support macros. huh? However, I did find the following: A Macro Preprocessor in C# _____________ Joe
-
Cool. I'm glad that you found a solution! I would have used a (yuck) macro. But, from what I could find, C# doesn't support macros. huh? However, I did find the following: A Macro Preprocessor in C# _____________ Joe
I just use the C-preprocesor. :rolleyes:
-
I just use the C-preprocesor. :rolleyes:
I agree, cpp rules! :) http://gcc.gnu.org/onlinedocs/cpp/[^] http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC1[^] In grad school, the assembler that I did (written in C) could do macros. How? By first running the assembler source code through cpp. :cool: Plus, the manual for cpp was already written. Double win! :) Heck, the smart engineer doesn't reinvent the wheel when it's just sitting there next to him/her! :) ___ Joe