Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a delegate from methodinfo

I have a drop down list that is populated by inspecting a class's methods and including those that match a specific signature. The problem is in taking the selected item from the list and getting the delegate to call that method in the class. The first method works, but I cannot figure out part of the second.

For example,

public delegate void MyDelegate(MyState state);  public static MyDelegate GetMyDelegateFromString(string methodName) {     switch (methodName)     {         case "CallMethodOne":             return MyFunctionsClass.CallMethodOne;         case "CallMethodTwo":             return MyFunctionsClass.CallMethodTwo;         default:             return MyFunctionsClass.CallMethodOne;     } }  public static MyDelegate GetMyDelegateFromStringReflection(string methodName) {     MyDelegate function = MyFunctionsClass.CallMethodOne;      Type inf = typeof(MyFunctionsClass);     foreach (var method in inf.GetMethods())     {         if (method.Name == methodName)         {             //function = method;             //how do I get the function to call?         }     }      return function; } 

How do I get the commented out section of the second method to work? How do I cast the MethodInfo into the delegate?

Thanks!

Edit: Here is the working solution.

public static MyDelegate GetMyDelegateFromStringReflection(string methodName) {     MyDelegate function = MyFunctionsClass.CallMethodOne;      Type inf = typeof(MyFunctionsClass);     foreach (var method in inf.GetMethods())     {         if (method.Name == methodName)         {             function = (MyDelegate)Delegate.CreateDelegate(typeof(MyDelegate), method);         }     }      return function; } 
like image 765
Ty. Avatar asked Jun 02 '09 16:06

Ty.


People also ask

What is the point of a delegate?

A delegate is a type-safe function pointer that can reference a method that has the same signature as that of the delegate. You can take advantage of delegates in C# to implement events and call-back methods. A multicast delegate is one that can point to one or more methods that have identical signatures.

Can delegates be private?

Just like classes and interfaces, we can declare delegates outside of classes or nested within classes. We can mark them private , public , or internal .

What is delegate C#?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

How do I create an instance of a delegate?

Create an instance of the delegate, using the CreateDelegate method. This method is static ( Shared in Visual Basic), so the delegate type must be supplied. Using the overloads of CreateDelegate that take a MethodInfo is recommended.

How to create a delegate with Dimdim mydelegate?

Dim myDelegate As delegate_OnClick = [ Delegate ].CreateDelegate ( GetType (delegate_OnClick), myInfo, True ) myDelegate (Repeat_Tastendruck_Control, EventArgs.Empty) NB: The type of the first parameter for the delegate must be the type where the method is declared, or a derived type.

How do I invoke a delegate from a form?

// Delegate^ dEmitted = handler->CreateDelegate (tDelegate); addHandler->Invoke (exFormAsObj, gcnew array<Object^> { dEmitted }); // Show the form. Clicking on the form causes the two // delegates to be invoked.

What is the first parameter type for a delegate?

NB: The type of the first parameter for the delegate must be the type where the method is declared, or a derived type. If you want to pass an interface, then the MethodInfo must come from the interface, not the class.


1 Answers

public static Delegate CreateDelegate(this MethodInfo methodInfo, object target) {     Func<Type[], Type> getType;     var isAction = methodInfo.ReturnType.Equals((typeof(void)));     var types = methodInfo.GetParameters().Select(p => p.ParameterType);      if (isAction) {         getType = Expression.GetActionType;     }     else {         getType = Expression.GetFuncType;         types = types.Concat(new[] { methodInfo.ReturnType });     }      if (methodInfo.IsStatic) {         return Delegate.CreateDelegate(getType(types.ToArray()), methodInfo);     }      return Delegate.CreateDelegate(getType(types.ToArray()), target, methodInfo.Name); } 
like image 160
Sagi Avatar answered Oct 07 '22 18:10

Sagi