Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
    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 13-Mar-12 1:38am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Martin P. Davies at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Martin P. Davies at 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.
   
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 at 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 at 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?
   
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?
Martin P. Davies at 13-Mar-12 8:56am
   
Thank you John/Bob, I'll have a bit of a play and see where we go - this is all really helpful, thank you very much.

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

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 13 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100