src/Mimp.SeeSharper.Reflection/MethodInfoExtensions.cs
/// <summary>
/// Return a delegate to invoke the method.
/// </summary>
/// <param name="method"></param>
/// <param name="delegateType"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException">If the delegate has no parameter for instance or the delegate hasn't equal parameter as the method.</exception>
/// <exception cref="InvalidOperationException">If the delegate types or instance type isn't castable to the method types.</exception>
Return a delegate to invoke the method.
Return a delegate to invoke the method .
public static Delegate GetInstanceDelegate(this MethodInfo method, Type delegateType)
        {
            if (method is null)
                throw new ArgumentNullException(nameof(method));
            if (delegateType is null)
                throw new ArgumentNullException(nameof(delegateType));
            var delegateParameterTypes = delegateType.GetDelegateParameterTypes().ToArray();
            if (delegateParameterTypes.Length < 1)
                throw new ArgumentException($@"Delegate ""{delegateType}"" has no parameter for the instance", nameof(delegateType));
            var paras = method.GetParameters().ToArray();
            if (delegateParameterTypes.Length - 1 != paras.Length)
                throw new ArgumentException($@"Delegate ""{delegateType}"" has an incorrect number of parameters", nameof(delegateType));
            var instance = Expression.Parameter(delegateParameterTypes[0], "instance");
            var parameters = new ParameterExpression[delegateParameterTypes.Length];
            parameters[0] = instance;
            for (var i = 1; i < parameters.Length; i++)
                parameters[i] = Expression.Parameter(delegateParameterTypes[i], $"arg{i - 1}");
            var arguments = new Expression[paras.Length];
            for (var i = 0; i < arguments.Length; i++)
                arguments[i] = Expression.Convert(
                        parameters[i + 1],
                        paras[i].ParameterType
                    );
            return Expression.Lambda(delegateType, Expression.Call(Expression.Convert(instance, method.ReflectedType!), method, arguments), parameters).Compile();
        }
public static Delegate GetInstanceDelegate ( this MethodInfo method , Type delegateType ) { if ( method is null ) throw new ArgumentNullException ( nameof ( method ) ) ; if ( delegateType is null ) throw new ArgumentNullException ( nameof ( delegateType ) ) ; var delegateParameterTypes = delegateType . GetDelegateParameterTypes ( ) . ToArray ( ) ; if ( delegateParameterTypes . Length < 1 ) throw new ArgumentException ( $@" Delegate  "" { delegateType } ""  has no parameter for the instance " , nameof ( delegateType ) ) ; var paras = method . GetParameters ( ) . ToArray ( ) ; if ( delegateParameterTypes . Length - 1 != paras . Length ) throw new ArgumentException ( $@" Delegate  "" { delegateType } ""  has an incorrect number of parameters " , nameof ( delegateType ) ) ; var instance = Expression . Parameter ( delegateParameterTypes [ 0 ] , " " ) ; var parameters = new ParameterExpression [ delegateParameterTypes . Length ] ; parameters [ 0 ] = instance ; for ( var i = 1 ; i < parameters . Length ; i ++ ) parameters [ i ] = Expression . Parameter ( delegateParameterTypes [ i ] , $" arg { i - 1 } " ) ; var arguments = new Expression [ paras . Length ] ; for ( var i = 0 ; i < arguments . Length ; i ++ ) arguments [ i ] = Expression . Convert ( parameters [ i + 1 ] , paras [ i ] . ParameterType ) ; return Expression . Lambda ( delegateType , Expression . Call ( Expression . Convert ( instance , method . ReflectedType ! ) , method , arguments ) , parameters ) . Compile ( ) ; }
Return a delegate to invoke the method.
Return a delegate to invoke the method .
None
None
If the delegate has no parameter for instance or the delegate hasn't equal parameter as the method.
If the delegate has no parameter for instance or the delegate hasn ' t equal parameter as the method .
If the delegate types or instance type isn't castable to the method types.
If the delegate types or instance type isn ' t castable to the method types .
MethodInfo
this
delegateType
Type
None
method
None