Mike, Thank you very much. Just what I needed. I missed the step that the MethodInfo of Invoke represents what the delegate is expecting.
Piewie. ----- Original Message ----- From: "Mike Woodring" <[EMAIL PROTECTED]> To: <[email protected]> Sent: Thursday, June 22, 2006 4:52 PM Subject: Re: [ADVANCED-DOTNET] Getting MemberInfo from a delegate |> Basically I want to write a function which accepts a | > typeof(MyDelegate) and | > is able to check if the MemberInfo of a method is bindable to | > this delegate. | | The code below takes a strict view of what it means to be compatible (same | return type and argument list). If you want something more along the lines | of the 2.0 model, you'll need to tweak it a bit. But you get the idea. | | -Mike | Bear Canyon Consulting LLC | http://www.bearcanyon.com | http://www.pluralsight.com/mike | | // The code below looks much better without | // Outlook-induced line wrapping... | | using System; | using System.Reflection; | | class Program | { | static void Main() | { | Type programType = typeof(Program); | Type delegateType = typeof(EventHandler); | | Console.WriteLine("CompatibleMethod : " + | IsMethodCompatibleWithDelegate(programType.GetMethod("CompatibleMethod"), | delegateType)); | Console.WriteLine("IncompatibleMethod1: " + | IsMethodCompatibleWithDelegate(programType.GetMethod("IncompatibleMethod1"), | delegateType)); | Console.WriteLine("IncompatibleMethod2: " + | IsMethodCompatibleWithDelegate(programType.GetMethod("IncompatibleMethod2"), | delegateType)); | } | | public static void CompatibleMethod( object sender, EventArgs args ) {} | public static void IncompatibleMethod1() {} | public static int IncompatibleMethod2( object sender, EventArgs args ) { | return(0); } | | static bool IsMethodCompatibleWithDelegate( MethodInfo targetMethod, Type | delegateType ) | { | if( !typeof(Delegate).IsAssignableFrom(delegateType) ) | { | throw new ArgumentException("The type specified must be derived from | System.Delegate", "delegateType"); | } | | MethodInfo delegateMethodInfo = delegateType.GetMethod("Invoke"); | | // Same return type? | // | if( !ReferenceEquals(delegateMethodInfo.ReturnType, | targetMethod.ReturnType) ) | { | return(false); | } | | // Same argument count? | // | ParameterInfo[] targetMethodArgs = targetMethod.GetParameters(); | ParameterInfo[] delegateMethodArgs = delegateMethodInfo.GetParameters(); | | if( targetMethodArgs.Length != delegateMethodArgs.Length ) | { | return(false); | } | | // Same argument types? | // | for( int n = 0; n < targetMethodArgs.Length; n++ ) | { | if( !ReferenceEquals(targetMethodArgs[n].ParameterType, | delegateMethodArgs[n].ParameterType) ) | { | return(false); | } | } | | // If we make it here, the target method has the same return type and | argument | // list that's required by the delegate. | // | return(true); | } | } | | =================================== | This list is hosted by DevelopMentorĀ® http://www.develop.com | | View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
