예제 #1
0
        public static object CreateInstance(Type type)
        {
            return(MethodFactory.ConstructorCache.GetOrAdd((object)type, (Func <object, MethodFactory.ConstructorCall>)(_ =>
            {
                ConstructorInfo constructor = ((IEnumerable <ConstructorInfo>)type.GetConstructors()).FirstOrDefault <ConstructorInfo>((Func <ConstructorInfo, bool>)(x => x.GetParameters().Length == 0));

                if (constructor == null)
                {
                    throw new ArgumentException("Type does not have any accessible parameterless constructors.");
                }
                return MethodFactory.CompileObjectConstructor(constructor);
            }))(new object[0]));
        }
예제 #2
0
        private static MethodFactory.ExpressionArgsPair GetObjectCreatorExpressions(ConstructorInfo constructor)
        {
            ParameterExpression args = Expression.Parameter(typeof(object[]), "args");

            Expression[]  callArgsExpressions = MethodFactory.GetMethodCallArgsExpressions(constructor.GetParameters(), args);
            NewExpression newExpression       = Expression.New(constructor, callArgsExpressions);

            return(new MethodFactory.ExpressionArgsPair()
            {
                Expression = (Expression)newExpression,
                Parameters = new ParameterExpression[1]
                {
                    args
                }
            });
        }
예제 #3
0
        private static MethodFactory.ExpressionArgsPair GetMethodCallExpressions(MethodInfo method)
        {
            ParameterExpression parameterExpression = Expression.Parameter(typeof(object), "instance");
            UnaryExpression     unaryExpression     = Expression.Convert((Expression)parameterExpression, method.DeclaringType);
            ParameterExpression args = Expression.Parameter(typeof(object[]), "args");

            Expression[] callArgsExpressions = MethodFactory.GetMethodCallArgsExpressions(method.GetParameters(), args);
            Expression   expression          = (Expression)Expression.Call(method.IsStatic ? (Expression)null : (Expression)unaryExpression, method, callArgsExpressions);

            if (method.ReturnType == typeof(void))
            {
                expression = (Expression)Expression.Block(expression, (Expression)Expression.Default(typeof(object)));
            }
            return(new MethodFactory.ExpressionArgsPair()
            {
                Expression = expression,
                Parameters = new ParameterExpression[2]
                {
                    parameterExpression,
                    args
                }
            });
        }
예제 #4
0
 public static MethodFactory.MethodCall CompileBoxedMethodCall(MethodInfo method)
 {
     MethodFactory.ExpressionArgsPair methodCallExpressions = MethodFactory.GetMethodCallExpressions(method);
     return(Expression.Lambda <MethodFactory.MethodCall>((Expression)Expression.Convert(methodCallExpressions.Expression, typeof(object)), methodCallExpressions.Parameters).Compile());
 }
예제 #5
0
 public static MethodFactory.MethodCall CompileMethodCall(MethodInfo method)
 {
     MethodFactory.ExpressionArgsPair methodCallExpressions = MethodFactory.GetMethodCallExpressions(method);
     return(Expression.Lambda <MethodFactory.MethodCall>(methodCallExpressions.Expression, methodCallExpressions.Parameters).Compile());
 }
예제 #6
0
 public static T CreateInstance <T>(Type type)
 {
     return((T)MethodFactory.CreateInstance(type));
 }
예제 #7
0
 public static MethodFactory.ConstructorCall CompileBoxedObjectConstructor(ConstructorInfo constructor)
 {
     MethodFactory.ExpressionArgsPair creatorExpressions = MethodFactory.GetObjectCreatorExpressions(constructor);
     return(Expression.Lambda <MethodFactory.ConstructorCall>((Expression)Expression.Convert(creatorExpressions.Expression, typeof(object)), creatorExpressions.Parameters).Compile());
 }
예제 #8
0
 public static object CallMethod(MethodInfo method, object instance, object[] arguments)
 {
     return(MethodFactory.MethodCache.GetOrAdd((object)method, (Func <object, MethodFactory.MethodCall>)(_ => MethodFactory.CompileMethodCall(method)))(instance, arguments));
 }
예제 #9
0
 public static T CallMethod <T>(MethodInfo method, object instance, object[] arguments)
 {
     return((T)MethodFactory.CallMethod(method, instance, arguments));
 }