Example #1
0
 public static Type[] TryGetTypeArgumentsFromBinder(InvokeMemberBinder binder)
 {
     if (SecuredReflection.IsAvailable)
     {
         var csharpInvoke = binder.GetType().GetInterfaces()
                            .FirstOrDefault(intf => intf.FullName == "Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder");
         if (csharpInvoke != null)
         {
             var typeArgs = (ICollection <Type>)SecuredReflectionMethods.GetProperty(csharpInvoke.GetProperty("TypeArguments"), binder, null);
             if (typeArgs != null && typeArgs.Count > 0)
             {
                 return(typeArgs.ToArray());
             }
         }
     }
     return(null);
 }
Example #2
0
        public static void RaiseEventThruReflection(object instance, EventInfo evt, object[] args)
        {
            MethodInfo raise;

            if ((raise = evt.GetRaiseMethod(true)) != null)
            {
                if (!raise.IsStatic && instance == null)
                {
                    throw new MockException("Unable to deduce the instance on which to raise the event");
                }

                //TODO: don't call reflection methods in GuardExternal when the profiler is working
                ProfilerInterceptor.GuardExternal(() => SecuredReflectionMethods.Invoke(raise, instance, args));
            }
            else
            {
                BindingFlags all   = BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
                var          field = evt.DeclaringType.GetField(evt.Name, all)               // C# event
                                     ?? evt.DeclaringType.GetField(evt.Name + "Event", all); //VB event

                if (field != null && field.FieldType == evt.EventHandlerType)
                {
                    if (!field.IsStatic && instance == null)
                    {
                        throw new MockException("Unable to deduce the instance on which to raise the event");
                    }

                    var handler = (Delegate)SecuredReflectionMethods.GetField(field, instance);
                    if (ProfilerInterceptor.IsProfilerAttached)
                    {
                        var invoker = MockingUtil.MakeFuncCaller(handler);
                        ProfilerInterceptor.GuardExternal(() => invoker(args, handler));
                    }
                    else
                    {
                        var invokeMethod = field.FieldType.GetMethod("Invoke", BindingFlags.Instance | BindingFlags.Public);
                        ProfilerInterceptor.GuardExternal(() => SecuredReflectionMethods.Invoke(invokeMethod, handler, args));
                    }
                }
            }
        }
        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));
        }