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; }
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.
Just like classes and interfaces, we can declare delegates outside of classes or nested within classes. We can mark them private , public , or internal .
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.
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.
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.
// 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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With