Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have an object with a delegate method set up. What I want to do is store a method name (in a database say), and then attach the method with that name to the object's delegate.

I have achieved this using reflection - example code is below:
C#
String ProgramName = "TestProgram";

ProgramClass MyObject = new ProgramClass();
MethodInfo ProgramInfo = MyObject.GetType().GetMethod(ProgramName);

Delegate del = Delegate.CreateDelegate(typeof(MyButton.ClickMethod), MyObject, ProgramInfo, false);

MyButton myButton = new MyButton();
myButton.Program = (MyButton.ClickMethod)del;


This does work, though it appears to be a little on the slow side. Essentially, I'm dynamically producing a load of buttons, and each one will have a 'program' attached to it, with is a method that is run when it is clicked - but this method can simply be to write a character (like an on-screen keyboard for example) - so although this method works, I need really a more immediate response.

Does anyone know of a better way to achieve this? Is the using a delegate not the best way?

To summarise - I'm wanting to store a method name in a database, and run that method when a button is pressed.

Any help greatly appreciated.
Posted

You can not avoid using reflection, but you can consider creating your delegates in advance (for example when your app starts), keep those delegates in a list or a Dictionary. Later you use this list to lookup methods and attach already created delegate.
 
Share this answer
 
Comments
Martin P. Davies 13-Mar-12 8:56am    
Thank you sjelen, I'll have a bit of a play and see where we go - this is all really helpful everyone, thank you very much.
I wouldn't store a method name in the database. I would store a (numeric) indicator that tells the code which method to use for the delegate. That way, the method name can be changed (if desired/required), and the database doesn't need to (and shouldn't need to) know about it.
 
Share this answer
 
Comments
Martin P. Davies 13-Mar-12 6:51am    
Hi John, thanks for the reply.

I agree - an indicator in the database would be a much better idea, but this still leaves me with the same problem - essentially I am still going to have find the name of the method and link it to the delegate, via an enum or something.

Failing that it'll be a large switch statement or something, but ideally I'd like to avoid that, though I'm beginning to think that it's probably the best route to take.
#realJSOP 13-Mar-12 7:28am    
But it will still be faster than using reflection, and it won't be tied to the database. You could also setup an in-memory map that allows you to index into an array to get the delegate method name.
BobJanova 13-Mar-12 8:28am    
Is a map of int->delegate really any clearer than storing the name of the delegate directly? I don't think so.
Dave Kreskowiak 13-Mar-12 8:58am    
Try making changes to the code or the delegate names to accomodate a new naming standard, then you'll find out just how maintainable the OP's solution really is. It's good in nothing EVER changes, but how often does that ever happen in code?
#realJSOP 13-Mar-12 9:32am    
Being clearer wasn't the goal. Being FASTER was the goal. Besides, if you used enums to represent the numeric values, wouldn't it still be as clear?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900