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.
Like this? (tested in GMail, just wondering?)
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");
// Compatible return type?
if( !delegateMethodInfo.ReturnType.IsAssignableFrom(targetMethod.ReturnType) )
{
return(false);
}
// Same argument count?
ParameterInfo[] targetMethodArgs = targetMethod.GetParameters();
ParameterInfo[] delegateMethodArgs = delegateMethodInfo.GetParameters();
if( targetMethodArgs.Length != delegateMethodArgs.Length )
{
return(false);
}
// Compatible argument types?
for( int n = 0; n < targetMethodArgs.Length; n++ )
{
if(
!targetMethodArgs[n].ParameterType.IsAssignableFrom(delegateMethodArgs[n].ParameterType)
)
{
return(false);
}
}
// If we make it here, the target method has the compatible return
type and argument
// list that's required by the delegate.
return(true);
}
--
"One of the main causes of the fall of the Roman Empire was that,
lacking zero, they had no way to indicate successful termination of
their C programs." --Robert Firth
Marc C. Brooks
http://musingmarc.blogspot.com
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com