Src/Black.Beard.ComponentModel/ComponentModel/Factories/ObjectActivator.cs
/// <summary>
/// Gets an customed method call factory for the specified ctor.
/// Note if the the generic is diferent of the declaring type of the ctor do a cast and is injected in the method.
/// </summary>
/// <typeparam name="T">is the type that containt the method</typeparam>
/// <param name="ctor">The ctor.</param>
/// <returns></returns>
Gets an customed method call factory for the specified ctor.
Note if the the generic is diferent of the declaring type of the ctor do a cast and is injected in the method.
Gets an customed method call factory for the specified ctor . Note if the the generic is diferent of the declaring type of the ctor do a cast and is injected in the method .
public static Factory<T> GetCallMethod<T>(MethodBase methodBase, MethodDescription description)
        where T : class
        {
            Type type = methodBase.DeclaringType;
            ParameterInfo[] paramsInfo = methodBase.GetParameters();
            ParameterExpression param = Expression.Parameter(typeof(object[]), "args");
            Expression[] argsExp = new Expression[paramsInfo.Length];
            for (int i = 0; i < paramsInfo.Length; i++)
            {
                Expression index = Expression.Constant(i);
                Type paramType = paramsInfo[i].ParameterType;
                Expression paramAccessorExp = Expression.ArrayIndex(param, index);
                Expression paramCastExp = Expression.Convert(paramAccessorExp, paramType);
                argsExp[i] = paramCastExp;
            }
            Expression newExp;
            if (methodBase is ConstructorInfo c)
                newExp = Expression.New(c, argsExp);
            else
                newExp = Expression.Call(null, (MethodInfo)methodBase, argsExp);
            if (methodBase.DeclaringType != typeof(T))
                newExp = Expression.Convert(newExp, typeof(T));
            LambdaExpression lambda = Expression.Lambda(typeof(ObjectActivator<T>), newExp, param);
            ObjectActivator<T> compiled = (ObjectActivator<T>)lambda.Compile();
            return new Factory<T>(compiled, methodBase, paramsInfo, description);
        }
public static Factory < T > GetCallMethod < T > ( MethodBase methodBase , MethodDescription description ) where T : class { Type type = methodBase . DeclaringType ; ParameterInfo [ ] paramsInfo = methodBase . GetParameters ( ) ; ParameterExpression param = Expression . Parameter ( typeof ( object [ ] ) , " " ) ; Expression [ ] argsExp = new Expression [ paramsInfo . Length ] ; for ( int i = 0 ; i < paramsInfo . Length ; i ++ ) { Expression index = Expression . Constant ( i ) ; Type paramType = paramsInfo [ i ] . ParameterType ; Expression paramAccessorExp = Expression . ArrayIndex ( param , index ) ; Expression paramCastExp = Expression . Convert ( paramAccessorExp , paramType ) ; argsExp [ i ] = paramCastExp ; } Expression newExp ; if ( methodBase is ConstructorInfo c ) newExp = Expression . New ( c , argsExp ) ; else newExp = Expression . Call ( null , ( MethodInfo ) methodBase , argsExp ) ; if ( methodBase . DeclaringType != typeof ( T ) ) newExp = Expression . Convert ( newExp , typeof ( T ) ) ; LambdaExpression lambda = Expression . Lambda ( typeof ( ObjectActivator < T > ) , newExp , param ) ; ObjectActivator < T > compiled = ( ObjectActivator < T > ) lambda . Compile ( ) ; return new Factory < T > ( compiled , methodBase , paramsInfo , description ) ; }
Gets an customed method call factory for the specified ctor.
Gets an customed method call factory for the specified ctor .
//create a single param of type object[] //pick each arg from the params array and create a typed expression of them //create a lambda with the New expression as body and our param object[] as arg //compile it
None
methodBase
MethodBase
description
MethodDescription
ctor
None
typeparam
None