src/Mimp.SeeSharper.Reflection/ConstructorInfoExtensions.cs
/// <summary>
/// Return a compiled delegate to invoke the constructor with a array of the parameters.
/// </summary>
/// <param name="constructor"></param>
/// <param name="delegateType"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException">If the delegate hasn't one parameter with a array as type.</exception>
/// <exception cref="InvalidOperationException">If the return type of the delegate isn't castable to the constructor reflected type.</exception>
Return a compiled delegate to invoke the constructor with a array of the parameters.
Return a compiled delegate to invoke the constructor with a array of the parameters .
public static Delegate GetParameterDelegate(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();
            if (delegateParameterTypes.Length != 1)
                throw new ArgumentException($@"Delegate ""{delegateType}"" has to have exact one parameter for the parameters", nameof(delegateType));
            var parametersType = delegateParameterTypes[0];
            if (!parametersType.IsArray)
                throw new ArgumentException($@"The type for the parameters has to be an array", nameof(delegateType));
            var parameters = Expression.Parameter(parametersType, "args");
            var paras = constructor.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.New(constructor, arguments), delegateType.GetDelegateReturnType())
            ), parameters).Compile();
        }
public static Delegate GetParameterDelegate ( 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 ( ) ; if ( delegateParameterTypes . Length != 1 ) throw new ArgumentException ( $@" Delegate  "" { delegateType } ""  has to have exact one parameter for the parameters " , nameof ( delegateType ) ) ; var parametersType = delegateParameterTypes [ 0 ] ; if ( ! parametersType . IsArray ) throw new ArgumentException ( $@" The type for the parameters has to be an array " , nameof ( delegateType ) ) ; var parameters = Expression . Parameter ( parametersType , " " ) ; var paras = constructor . 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 . New ( constructor , arguments ) , delegateType . GetDelegateReturnType ( ) ) ) , parameters ) . Compile ( ) ; }
Return a compiled delegate to invoke the constructor with a array of the parameters.
Return a compiled delegate to invoke the constructor with a array of the parameters .
None
None
If the delegate hasn't one parameter with a array as type.
If the delegate hasn ' t one parameter with a array as type .
If the return type of the delegate isn't castable to the constructor reflected type.
If the return type of the delegate isn ' t castable to the constructor reflected type .
ConstructorInfo
this
delegateType
Type
None
constructor
None