Esempio n. 1
0
        public static object EvaluateExpression(this Expression expr)
        {
            while (expr.NodeType == ExpressionType.Convert)
            {
                var unary = expr as UnaryExpression;
                if (unary.Type.IsAssignableFrom(unary.Operand.Type))
                {
                    expr = unary.Operand;
                }
                else
                {
                    break;
                }
            }

            var constant = expr as ConstantExpression;

            if (constant != null)
            {
                return(constant.Value);
            }

            bool canCallGetField = true;

#if COREFX
            canCallGetField = ProfilerInterceptor.IsProfilerAttached;
#endif
            if (canCallGetField)
            {
                var memberAccess = expr as MemberExpression;
                if (memberAccess != null)
                {
                    var asField = memberAccess.Member as FieldInfo;
                    if (asField != null)
                    {
                        return(SecuredReflectionMethods.GetField(asField, memberAccess.Expression != null
                                                        ? memberAccess.Expression.EvaluateExpression() : null));
                    }
                }
            }

#if !DOTNET35
            var listInit = expr as ListInitExpression;
            if (listInit != null)
            {
                var collection = Expression.Variable(listInit.NewExpression.Type);

                var block = new List <Expression>
                {
                    Expression.Assign(collection, listInit.NewExpression)
                };
                block.AddRange(listInit.Initializers.Select(init => Expression.Call(collection, init.AddMethod, init.Arguments.ToArray())));
                block.Add(collection);

                expr = Expression.Block(new[] { collection }, block.ToArray());
            }
#endif

            var lambda = Expression.Lambda(Expression.Convert(expr, typeof(object)));
            var delg   = (Func <object>)lambda.Compile();

            return(ProfilerInterceptor.GuardExternal(delg));
        }
Esempio n. 2
0
        public static object CreateObject(this Type type, object[] args)
        {
            args = args ?? NoObjects;

            var constructorInfos = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            if (constructorInfos.Length == 0 || (type.IsValueType && args.Length == 0))
            {
                if (args.Length > 0)
                {
                    throw new MockException("Type has no non-default constructors.");
                }

                return(type.GetDefaultValue());
            }

            object state;
            var    ctor = (ConstructorInfo)MockingUtil.BindToMethod(MockingUtil.Default,
                                                                    constructorInfos, ref args, null, null, null, out state);

            var ctorParameters = ctor.GetParameters();

            for (int i = 0; i < ctorParameters.Length; ++i)
            {
                var paramType = ctorParameters[i].ParameterType;
                if (paramType.IsValueType && args[i] == null)
                {
                    args[i] = paramType.GetDefaultValue();
                }
                else if (args[i] != null && !paramType.IsAssignableFrom(args[i].GetType()))
                {
                    args[i] = Convert.ChangeType(args[i], paramType, System.Globalization.CultureInfo.CurrentCulture);
                }
            }

#if !PORTABLE
            var newCall = MockingUtil.CreateDynamicMethod <Func <object[], object> >(il =>
            {
                il.UnpackArgArray(OpCodes.Ldarg_0, ctor);
                il.PushArgArray(ctor);
                il.Emit(OpCodes.Newobj, ctor);
                if (type.IsValueType)
                {
                    il.Emit(OpCodes.Box, type);
                }
                il.Emit(OpCodes.Ret);
            });

            return(ProfilerInterceptor.GuardExternal(() =>
            {
                try
                {
                    return newCall(args);
                }
                catch (MemberAccessException ex)
                {
                    GC.KeepAlive(ex);
                    return MockingUtil.CreateInstance(type, args);
                }
            }));
#else
            return(MockingUtil.CreateInstance(type, args));
#endif
        }