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