public ref TRefReturn GetProperty <TRefReturn>(string name) { return(ref ProfilerInterceptor.GuardInternal((target, arguments) => { var prop = MockingUtil.ResolveProperty(this.type, name, false, null, this.instance != null); var method = prop.GetGetMethod(true); ProfilerInterceptor.RefReturn <TRefReturn> @delegate = MockingUtil.CreateDynamicMethodInvoker <TRefReturn>(target, method, arguments); return ref ProfilerInterceptor.GuardExternal(@delegate, target, arguments); }, this.instance, MockingUtil.NoObjects)); }
public ref TRefReturn CallMethod <TRefReturn>(string name, params object[] args) { return(ref ProfilerInterceptor.GuardInternal((target, arguments) => { arguments = arguments ?? MockingUtil.NoObjects; var candidates = type.GetAllMethods() .Where(m => m.Name == name && MockingUtil.CanCall(m, this.instance != null)) .Select(m => MockingUtil.TrySpecializeGenericMethod(m, arguments.Select(a => a != null ? a.GetType() : null).ToArray()) ?? m) .ToArray(); object state; var method = MockingUtil.BindToMethod(MockingUtil.AllMembers, candidates, ref arguments, null, null, null, out state); ProfilerInterceptor.RefReturn <TRefReturn> @delegate = MockingUtil.CreateDynamicMethodInvoker <TRefReturn>(target, method as MethodInfo, arguments); return ref ProfilerInterceptor.GuardExternal(@delegate, target, arguments); }, this.instance, args ?? MockingUtil.NoObjects)); }
protected override Expression VisitConstant(ConstantExpression c) { if (c.Type.IsCompilerGenerated()) { return(Expression.Parameter(c.Type, "__compiler_generated__")); } if (!hasMockReplacement && c.Type.IsProxy() || (c.Value != null && c.Value.GetType().IsProxy())) { hasMockReplacement = true; var param = Expression.Parameter(c.Type, "x"); this.Parameters.Add(param); return(param); } if (c.Value != null) { string name; var value = c.Value; if (!this.stringReplacements.TryGetValue(value, out name)) { name = String.Format("__string_replacement_{0}__", this.stringReplacements.Count); this.stringReplacements.Add(value, name); var valueStr = "<Exception>"; try { valueStr = ProfilerInterceptor.GuardExternal(() => value.ToString()); } catch { } if (String.IsNullOrEmpty(valueStr)) { valueStr = "#" + this.stringReplacements.Count; } this.ReplacementValues.Add(name, valueStr); } var param = Expression.Parameter(c.Type, name); this.Parameters.Add(param); return(param); } return(base.VisitConstant(c)); }
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 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 }
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)); }