I really wish I could either a) pass an object and a methodinfo as constructor parameters to a delegate or b) discover the metadata token for a method from managed code. If I could do a) then I could say this:
uint mydelegate(); object o = new object(); mydelegate d = new mydelegate(o, o.GetType().GetMethod("GetHashCode")); uint hashcode = d(); Here's an example of why I care. I have a class that implements a bunch of related methods (all with the same signature). This class stores delegates to these methods in a hashtable and has a function like: void Call(string label) Said class uses the label to key to the desired function. Now, given the constructor above I could write some really cool code, namely: public HypotheticalClassCtor() { foreach(MethodInfo m in this.GetType().GetMethods()) { if(m.Name.IndexOf("Routine_") != -1) { //Impossible to do from C#! MyHashTable[m.Name.Substring(9)] = new SubroutineDelegate(this,m); } } } void Call(string label) { return MyHashTable[label](); } This snazzy bit of code would automatically add any routine in my class that fit a certain naming convention to the hashtable. As it is I have to manually put a line in the constructor for every routine I want added. This is functional but isn't very sexy :) I can get there from here if I can have (b). I can do what I want if there any way to get the metadata token from a MethodInfo. Then I could write a little library in IL that would expose a function that would explicitly create the delegate with the two parameters that C# is already using. For future versions of the CLR it would be awesome if delegates just got a constructor that could take a MethodInfo like this. Clearly the MethodInfo has the right token buried in there *somewhere*... Any takers? Jason You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.