How to create an empty delegate using Expression T

2019-03-29 00:59发布

问题:

Using anonymous methods you can create empty delegates since C# 2.0.

public event EventHandler SomeEvent = delegate {};
public event Action OtherEvent = delegate {};

This is e.g. useful to prevent having to do the null check when invoking events.

How can I create the same behavior using Expression Trees?

The only possible option I see now is to use Expression.Lambda(), but as far as I can tell this would require a lot of extra work.

回答1:

An expression tree, by nature of its purpose, always has an expression for a body rather than a statement in the original design.

In C# 3 there was no way at all to express an expression tree whose body is an empty statement block. More recently, the expression tree library has been extended to allow for statements, but the C# semantic analysis rules were not updated to take advantage of that; you still cannot turn a statement lambda into an expression tree.



回答2:

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();


回答3:

Expanding the Steven answer a little bit - I needed similar functionality to create an empty delegate for both - Action and Func types - following is the helper that I created for that task:

    static class MethodInfoHelper<T>
    {
        static MethodInfoHelper()
        {
            VerifyTypeIsDelegate();
        }

        public static void VerifyTypeIsDelegate()
        {
            //Lets make sure this is only ever used in code for Func<> types
            if (!typeof(T).IsSubclassOf(typeof(Delegate)))
            {
                throw new InvalidOperationException(typeof(T).Name + " is not a delegate type");
            }

            if (!typeof(T).Name.StartsWith("Func") && !typeof(T).Name.StartsWith("Action"))
            {
                throw new InvalidOperationException(typeof(T).Name + " is not a Func nor an Action");
            }
        }

        private static bool HasReturnType
        {
            get { return typeof(T).Name.StartsWith("Func"); }
        }

        /// <summary>
        /// Creates an empty delegate of given type
        /// </summary>
        /// <typeparam name="T">Func or Action type to be created</typeparam>
        /// <returns>A delegate to expression doing nothing</returns>
        public static T CreateEmptyDelegate()
        {
            Type funcType = typeof(T);
            Type[] genericArgs = funcType.GenericTypeArguments;

            List<ParameterExpression> paramsExpressions = new List<ParameterExpression>();
            for (int paramIdx = 0; paramIdx < (HasReturnType ? genericArgs.Length - 1 : genericArgs.Length); paramIdx++)
            {
                Type argType = genericArgs[paramIdx];

                ParameterExpression argExpression = Expression.Parameter(argType, "arg" + paramIdx);
                paramsExpressions.Add(argExpression);
            }

            Type returnType = HasReturnType ? genericArgs.Last() : typeof(void);

            DefaultExpression emptyExpression = (DefaultExpression)typeof(DefaultExpression).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null,
                new Type[] { typeof(Type) }, null).Invoke(new[] { returnType });

            Expression<T> resultingExpression = Expression.Lambda<T>(
                emptyExpression, "EmptyDelegate", true, paramsExpressions);

            return resultingExpression.Compile();
        }
    }