public InterceptInfo(DuringCallbackEventArgs e) { this.TargetInstance = e.TargetInstance; this.TypeName = e.TypeName; this.MethodName = e.MethodName; this.GenericParams = e.GenericParams; }
public override void MethodBehavior(DuringCallbackEventArgs e) { this.Result = this.CallOriginalMethod(e); base.MethodBehavior(e); e.MethodBehavior = MethodBehaviors.ReturnsCustomValue; e.ReturnValueOrException = this.Result; }
public static DuringCallbackEventArgs CreateMissingMethodArgsFrom(DuringCallbackEventArgs e) { //Debug.Assert(e.ParameterValues.Length==7,"Am not dealing with a LateBind call"); //method signature: //LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) // (0 , 1 NULL , 2 , 3 , 4 NULL , 5 NULL , 6 NULL , 7) DuringCallbackEventArgs newArgs = GetMissingMethodArgsFromLateCallArgs(e); return newArgs; }
private static DuringCallbackEventArgs GetMissingMethodArgsFromLateCallArgs(DuringCallbackEventArgs e) { return new DuringCallbackEventArgs() { ParameterValues = (object[])e.ParameterValues[3], MethodName = (string)e.ParameterValues[2], TypeName = e.ParameterValues[1]== null ? "":e.ParameterValues[1].ToString(), TargetInstance = e.ParameterValues[0], GenericParams = e.ParameterValues[5] == null ? new Type[0] : (Type[])e.ParameterValues[5] }; }
// // public static MethodBehaviors NotifyParties(Action<MissingMethodEventArgs> action) // { // return MethodBehaviors.IDontCare; // } public static bool HasMissingMethodImpl(DuringCallbackEventArgs e) { var missingArgs = GetMissingMethodArgsFromLateCallArgs(e); if (missingArgs.TargetInstance!=null ) { MethodInfo method = missingArgs.TargetInstance.GetType().GetMethod("missing_method",missingSearchFlags); if (method !=null) { return true; } } return false; }
public static bool IsMissing(DuringCallbackEventArgs e) { if (e.TypeName == "Microsoft.VisualBasic.CompilerServices.NewLateBinding" && e.MethodName.StartsWith("Late") ) { object theInstance = e.ParameterValues[0]; string memberName = (string) e.ParameterValues[2]; return theInstance != null && theInstance.GetType().GetMember(memberName).Length == 0; } //method signature: //LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) // (0 , 1 NULL , 2 , 3 , 4 NULL , 5 NULL , 6 NULL , 7) return false; }
public static void InvokeMissingMethod(DuringCallbackEventArgs e) { var missingArgs = GetMissingMethodArgsFromLateCallArgs(e); MethodInfo method = missingArgs.TargetInstance.GetType().GetMethod("missing_method", missingSearchFlags); MissingMethodArgs args = MissingMethodArgs.FromLateCallArguments(e); var retVal = method.Invoke(missingArgs.TargetInstance, new object[] {args}); if (false) { e.MethodBehavior=MethodBehaviors.SkipActualMethod; } else { e.MethodBehavior= MethodBehaviors.ReturnsCustomValue; e.ReturnValueOrException = retVal; } }
public static MissingMethodArgs FromLateCallArguments(DuringCallbackEventArgs e) { //method signature: //LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) // (0 , 1 NULL , 2 , 3 , 4 NULL , 5 NULL , 6 NULL , 7) MissingMethodArgs @new = new MissingMethodArgs() { Arguments = (object[])e.ParameterValues[3], MemberName = (string)e.ParameterValues[2], Type = e.ParameterValues[1] == null ? null : e.ParameterValues[1] as Type, TargetInstance = e.ParameterValues[0], TypeArguments = e.ParameterValues[5] == null ? new Type[0] : (Type[])e.ParameterValues[5], ArgumentNames = e.ParameterValues[4] == null ? new string[0] : (string[])e.ParameterValues[4], // IgnoreReturn = Convert.ToBoolean(e.ParameterValues[7] ) }; return @new; }
protected virtual string GetMessage(DuringCallbackEventArgs e) { string message = string.Empty; try { string parameters = GetParameters(e); message = string.Concat(e.TypeName, ".", e.MethodName, "(", parameters, ")"); for (int i = 11; i <= 11 + _depth - 1; i++) { var frame = new StackFrame(i); var method = frame.GetMethod(); if (method != null) { var type = (method.DeclaringType != null ? method.DeclaringType.FullName : "(?)"); // weird, but sometimes method.DeclaringType *is* null (method.Name is "lambda_method") message += Environment.NewLine + "--> " + type + "." + method.Name; } } } catch (Exception ex) { message += Environment.NewLine + ex.ToString() + Environment.NewLine; } return message; }
protected override string GetMessage(DuringCallbackEventArgs e) { return base.GetMessage(e) + GetResultMessage(); }
private void Handle(DuringCallbackEventArgs e, Action<DuringCallbackEventArgs> handler) { if (_aspect.ShouldIntercept(new InterceptInfo(e)) && _aspect.Active) handler(e); }
internal void HandleStaticConstructor(DuringCallbackEventArgs e) { Handle(e, _aspect.StaticConstructorBehavior); }
internal void HandleMissingMethod(DuringCallbackEventArgs e) { Handle(e, _aspect.MissingMethodBehavior); }
private static MethodBehaviors InvokeDuringEventWithBehaviorDecision(Action<DuringCallbackEventArgs> call, DuringCallbackEventArgs args) { if (call == null) { return MethodBehaviors.IDontCare; } bool wasLastOptionChangedAtLeastOnce = false; MethodBehaviors lastOption = args.MethodBehavior; Aspect lastAspect = null; Delegate[] list = call.GetInvocationList(); foreach (Delegate d2 in list) { try { Action<DuringCallbackEventArgs> invokeEvent = d2 as Action<DuringCallbackEventArgs>; args.MethodBehavior = MethodBehaviors.IDontCare; args.WasBehaviorSet = false; invokeEvent(args); bool wasRealBehaviorSet = args.WasBehaviorSet && args.MethodBehavior != MethodBehaviors.IDontCare; bool isNewBehaviorConflictingWithOlderBehaviorRequest = wasRealBehaviorSet && lastOption != MethodBehaviors.IDontCare; bool isThisTheFirstRealBehaviorSelection = wasRealBehaviorSet && !wasLastOptionChangedAtLeastOnce; //var aspect = GetAspectHandledBy(invokeEvent); if (isThisTheFirstRealBehaviorSelection) { wasLastOptionChangedAtLeastOnce = true; lastAspect = GetAspectHandledBy(invokeEvent); lastOption = args.MethodBehavior; continue; } if (isNewBehaviorConflictingWithOlderBehaviorRequest) { Aspect currentAspect = GetAspectHandledBy(invokeEvent); string msg = BuildConflictMessage(args, lastOption, lastAspect, currentAspect); throw new ConflictingBehaviorException(msg); } if (args.MethodBehavior != MethodBehaviors.IDontCare) { lastAspect = GetAspectHandledBy(invokeEvent); lastOption = args.MethodBehavior; } } catch (Exception e) { args.MethodBehavior = MethodBehaviors.ThrowException; args.ReturnValueOrException = e; return MethodBehaviors.ThrowException; } } return lastOption; }
protected virtual void DoTrace(DuringCallbackEventArgs e) { string message = GetMessage(e); DoTrace(message); }
private static string BuildConflictMessage(DuringCallbackEventArgs args, MethodBehaviors lastOption, Aspect lastAspect, Aspect currentAspect) { return string.Format( @"Two aspects requested conflicting behavior. Method:{0}.{1} Aspect {2} requested {3} Aspect {4} requested {5}", args.TypeName, args.MethodName, lastAspect.GetType().Name, lastOption, currentAspect.GetType().Name, args.MethodBehavior); }
public override void MethodBehavior(DuringCallbackEventArgs e) { Write("skipping method {0}", e.MethodName); e.MethodBehavior=MethodBehaviors.SkipActualMethod; }
public override void ConstructorBehavior(DuringCallbackEventArgs e) { DoTrace(e); }
public override void MissingMethodBehavior(DuringCallbackEventArgs e) { OnMissingMethodBehaviorCallback(e); }
protected virtual string GetParameters(DuringCallbackEventArgs e) { var list = new List<string>(); if (e.ParameterValues != null) { foreach (var param in e.ParameterValues) { var value = param != null ? param.ToString() : "null"; list.Add(value); } } return string.Join(", ", list.ToArray()); }
public override void StaticConstructorBehavior(DuringCallbackEventArgs e) { Write("skipping static ctor {0}", e.TypeName); e.MethodBehavior = MethodBehaviors.SkipActualMethod; }
private static void CallBackDuringMethodCall(TMDuringCallEventArgs eventArgs) { Type[] types = new Type[0]; TypeParams tp = eventArgs.MethodGenericParameters as TypeParams; if (tp != null) { types = tp.GetTypes(); } DuringCallbackEventArgs e = new DuringCallbackEventArgs() { GenericParams = types, MethodBehavior = MethodBehaviors.IDontCare, MethodName = eventArgs.MethodName, TargetInstance = eventArgs.Context, TypeName = eventArgs.TypeName, ReturnValueOrException = null, ParameterValues = eventArgs.Parameters }; Action<DuringCallbackEventArgs> eventTofire = duringMethodCall; MethodBehaviors decision = MethodBehaviors.IDontCare; if (eventArgs.MethodName == ".ctor") { eventTofire = DuringConstructorCall; } if (eventArgs.MethodName == ".cctor") { // Console.WriteLine("Checking {0}.{1}", e.TypeName, e.MethodName); eventTofire = DuringStaticConstructorCall; } if (MissingMethodHelper.IsMissing(e)) { if (MissingMethodHelper.HasMissingMethodImpl(e)) { MissingMethodHelper.InvokeMissingMethod(e); decision = e.MethodBehavior; } else { e = MissingMethodHelper.CreateMissingMethodArgsFrom(e); decision = InvokeDuringEventWithBehaviorDecision(missingMethodCall, e); } } else { decision = InvokeDuringEventWithBehaviorDecision(eventTofire, e); } // Console.WriteLine("- Decided {2} on {0}.{1}", typeName, methodName,decision); switch (decision) { case MethodBehaviors.IDontCare: eventArgs.Returns(IsolationBehavior.LetIsolatorDecide); break; case MethodBehaviors.CallRealMethod: // Console.WriteLine("INVOKING {0}.{1}", typeName, methodName); eventArgs.ReturnsValue(MockManager.CONTINUE_WITH_METHOD); break; case MethodBehaviors.SkipActualMethod: // Console.WriteLine("Skipping {0}.{1}" , typeName,methodName); eventArgs.Returns(IsolationBehavior.SkipMethod); break; case MethodBehaviors.ReturnsCustomValue: eventArgs.ReturnsValue(e.ReturnValueOrException); break; case MethodBehaviors.ThrowException: eventArgs.ThrowsException(e.ReturnValueOrException as Exception); break; } }
public override void ConstructorBehavior(DuringCallbackEventArgs e) { OnCTorBehaviorCallback(e); }
public override void MethodBehavior(DuringCallbackEventArgs e) { DoTrace(e); }
public override void StaticConstructorBehavior(DuringCallbackEventArgs e) { OnStaticCtorBehaviorCallback(e); }
public override void MethodBehavior(DuringCallbackEventArgs e) { _capturedMethodInfo = (MethodInfo) this.GetInterceptedMethod(e); this.CallOriginalMethod(e); base.MethodBehavior(e); }