From 3e9337d9fe7c937290f6c1984c5fe8d12a3d1662 Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Thu, 14 Feb 2008 11:32:11 +0000 Subject: [PATCH] 2008-02-14 Jb Evain * Delegate.cs (CreateDelegate): refactor. DRY! svn path=/trunk/mcs/; revision=95619 --- mcs/class/corlib/System/ChangeLog | 4 ++ mcs/class/corlib/System/Delegate.cs | 86 +++++++++-------------------- 2 files changed, 30 insertions(+), 60 deletions(-) diff --git a/mcs/class/corlib/System/ChangeLog b/mcs/class/corlib/System/ChangeLog index 8d2f251a488..8810f888c7f 100644 --- a/mcs/class/corlib/System/ChangeLog +++ b/mcs/class/corlib/System/ChangeLog @@ -1,3 +1,7 @@ +2008-02-14 Jb Evain + + * Delegate.cs (CreateDelegate): refactor. DRY! + 2008-02-12 Gert Driesen * Delegate.cs (CreateDelegate): Walk the inheritance change to find diff --git a/mcs/class/corlib/System/Delegate.cs b/mcs/class/corlib/System/Delegate.cs index 872c0387cdc..9d5964696c3 100644 --- a/mcs/class/corlib/System/Delegate.cs +++ b/mcs/class/corlib/System/Delegate.cs @@ -277,19 +277,11 @@ namespace System return CreateDelegate(type, target, method, false); } -#if NET_2_0 - public -#else - internal -#endif - static Delegate CreateDelegate (Type type, Type target, string method, bool ignoreCase, bool throwOnBindFailure) + static MethodInfo GetCandidateMethod (Type type, Type target, string method, BindingFlags bflags, bool ignoreCase, bool throwOnBindFailure) { if (type == null) throw new ArgumentNullException ("type"); - if (target == null) - throw new ArgumentNullException ("target"); - if (method == null) throw new ArgumentNullException ("method"); @@ -314,8 +306,9 @@ namespace System * inherited methods */ BindingFlags flags = BindingFlags.ExactBinding | - BindingFlags.Public | BindingFlags.Static | - BindingFlags.NonPublic | BindingFlags.DeclaredOnly; + BindingFlags.Public | BindingFlags.NonPublic | + BindingFlags.DeclaredOnly | bflags; + if (ignoreCase) flags |= BindingFlags.IgnoreCase; @@ -332,11 +325,29 @@ namespace System if (info == null) { if (throwOnBindFailure) - throw new ArgumentException ("Couldn't bind to method."); + throw new ArgumentException ("Couldn't bind to method '" + method + "'."); else return null; } + return info; + } + +#if NET_2_0 + public +#else + internal +#endif + static Delegate CreateDelegate (Type type, Type target, string method, bool ignoreCase, bool throwOnBindFailure) + { + if (target == null) + throw new ArgumentNullException ("target"); + + MethodInfo info = GetCandidateMethod (type, target, method, + BindingFlags.Static, ignoreCase, throwOnBindFailure); + if (info == null) + return null; + return CreateDelegate_internal (type, null, info); } @@ -357,58 +368,13 @@ namespace System #endif static Delegate CreateDelegate (Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure) { - if (type == null) - throw new ArgumentNullException ("type"); - if (target == null) throw new ArgumentNullException ("target"); - if (method == null) - throw new ArgumentNullException ("method"); - - if (!type.IsSubclassOf (typeof (MulticastDelegate))) - throw new ArgumentException ("type"); - - MethodInfo invoke = type.GetMethod ("Invoke"); - ParameterInfo[] delargs = invoke.GetParameters (); - Type[] delargtypes = new Type [delargs.Length]; - - for (int i=0; i