src/Mimp.SeeSharper.Reflection/MethodInfoExtensions.cs
/// <summary>
/// Return a delegate to invoke the method with the instance and a array of parameters.
/// </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 and the parameters.</exception>
/// <exception cref="InvalidOperationException">If the return type or instance type isn't castable to the delegate types.</exception>
Return a delegate to invoke the method with the instance and a array of parameters.
Return a delegate to invoke the method with the instance and a array of parameters .
public static Delegate GetInstanceParameterDelegate(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 != 2)
                throw new ArgumentException($@"Delegate ""{delegateType}"" has to have 2 parameters - instance and parameters", nameof(delegateType));
            if (!delegateParameterTypes[1].IsArray)
                throw new ArgumentException($@"The type for the parameters has to be an array", nameof(delegateType));
            var instance = Expression.Parameter(delegateParameterTypes[0], "instance");
            var parameters = Expression.Parameter(delegateParameterTypes[1], "args");
            var paras = method.GetParameters();
            var arguments = new Expression[paras.Length];
            for (var i = 0; i < arguments.Length; i++)
                arguments[i] = Expression.Convert(
                        Expression.ArrayIndex(parameters, Expression.Constant(i)),
                        paras[i].ParameterType
                    );
            return Expression.Lambda(delegateType, Expression.Block(
                Expression.IfThen(
                    Expression.NotEqual(Expression.ArrayLength(parameters), Expression.Constant(arguments.Length)),
                    Expression.Throw(
                        Expression.New(typeof(TargetParameterCountException).GetConstructor(new[] { typeof(string) })!,
                        Expression.Constant($"An incorrect number of parameters was passed.")))
                ),
                Expression.Convert(Expression.Call(Expression.Convert(instance, method.ReflectedType!), method, arguments), delegateType.GetDelegateReturnType())
            ), instance, parameters).Compile();
        }
public static Delegate GetInstanceParameterDelegate ( 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 != 2 ) throw new ArgumentException ( $@" Delegate  "" { delegateType } ""  has to have 2 parameters - instance and parameters " , nameof ( delegateType ) ) ; if ( ! delegateParameterTypes [ 1 ] . IsArray ) throw new ArgumentException ( $@" The type for the parameters has to be an array " , nameof ( delegateType ) ) ; var instance = Expression . Parameter ( delegateParameterTypes [ 0 ] , " " ) ; var parameters = Expression . Parameter ( delegateParameterTypes [ 1 ] , " " ) ; var paras = method . GetParameters ( ) ; var arguments = new Expression [ paras . Length ] ; for ( var i = 0 ; i < arguments . Length ; i ++ ) arguments [ i ] = Expression . Convert ( Expression . ArrayIndex ( parameters , Expression . Constant ( i ) ) , paras [ i ] . ParameterType ) ; return Expression . Lambda ( delegateType , Expression . Block ( Expression . IfThen ( Expression . NotEqual ( Expression . ArrayLength ( parameters ) , Expression . Constant ( arguments . Length ) ) , Expression . Throw ( Expression . New ( typeof ( TargetParameterCountException ) . GetConstructor ( new [ ] { typeof ( string ) } ) ! , Expression . Constant ( $" An incorrect number of parameters was passed. " ) ) ) ) , Expression . Convert ( Expression . Call ( Expression . Convert ( instance , method . ReflectedType ! ) , method , arguments ) , delegateType . GetDelegateReturnType ( ) ) ) , instance , parameters ) . Compile ( ) ; }
Return a delegate to invoke the method with the instance and a array of parameters.
Return a delegate to invoke the method with the instance and a array of parameters .
None
None
If the delegate has no parameter for instance and the parameters.
If the delegate has no parameter for instance and the parameters .
If the return type or instance type isn't castable to the delegate types.
If the return type or instance type isn ' t castable to the delegate types .
MethodInfo
this
delegateType
Type
None
method
None