As it turns out it isn't that much work using Expression.Lambda()
. However, I'm still interested in possible other answers.
I did need a helper method which I wrote previously:
/// <summary>
/// The name of the Invoke method of a Delegate.
/// </summary>
const string InvokeMethod = "Invoke";
/// <summary>
/// Get method info for a specified delegate type.
/// </summary>
/// <param name = "delegateType">The delegate type to get info for.</param>
/// <returns>The method info for the given delegate type.</returns>
public static MethodInfo MethodInfoFromDelegateType( Type delegateType )
{
Contract.Requires(
delegateType.IsSubclassOf( typeof( MulticastDelegate ) ),
"Given type should be a delegate." );
return delegateType.GetMethod( InvokeMethod );
}
When you have EventInfo
you can create an empty lambda for it as follows:
EventInfo _event;
...
MethodInfo delegateInfo
= DelegateHelper.MethodInfoFromDelegateType( _event.EventHandlerType );
ParameterExpression[] parameters = delegateInfo
.GetParameters()
.Select( p => Expression.Parameter( p.ParameterType ) )
.ToArray();
Delegate emptyDelegate = Expression.Lambda(
_event.EventHandlerType,
Expression.Empty(), "EmptyDelegate", true, parameters ).Compile();