Delegate.CreateDelegate ArgumentException not helpful!
I have some code that is supposed to return a delegate of a certain type:
private static RuleSet.RuleDelegate<MemberType> _GetMemberRuleMethodDelegate<MemberType>(string methodName, Type ruleType)
{
MethodInfo mi = ruleType.GetMethod(methodName);
if (mi != null)
{
if (mi.IsPrivate)
throw new FastReflectorFailException(String.Format(
"Rule: '{0}' is private, and cannot be executed directly.", methodName));
return (RuleSet.RuleDelegate<MemberType>)
Delegate.CreateDelegate(typeof(RuleSet.RuleDelegate<MemberType>), mi);
}
else
throw new FastReflectorFailException(String.Format(
"Rule: '{0}' does not exist.", methodName));
}
The idea is that I grab the method by it's string name (methodName), from the Type ruleType. It gets a MethodInfo object, and if the method isn't private, it tries to return a delegate of type RuleDelegate<>, whose signature the methodInfo variable matches (I know for certain it matches because this is a limited test case).
On this line, the code throws an ArgumentException error:
return (RuleSet.RuleDelegate<MemberType>)
Delegate.CreateDelegate(typeof(RuleSet.RuleDelegate<MemberType>), mi);
It says "Error binding to target method." Thanks, I can see that.
Anyway, in the metadata of the CreateDelegate method, I found this note about ArgumentException:
// System.ArgumentException:
// type does not inherit System.MulticastDelegate.-or-type is not a RuntimeType.
// See Runtime Types in Reflection. -or- method is not a static method, and
// the .NET Framework version is 1.0 or 1.1. -or-method cannot be bound.-or-method
// is not a RuntimeMethodInfo. See Runtime Types in Reflection.
I don't know how to tell whether the type inherits from System.MulticastDelegate.
I don't know how to tell whether it's a RuntimeType (but I suspect it is).
I'm using .NET 2.0, so that's not the issue.
I know why the method would be unable to bind.
I know how how to tell whether the method is a RuntimeMethodInfo.
I looked at "Runtime Types in Reflection" on msdn, and it's worthless.
Can someone point me in the right direction?