public void BeforeInvoke(ICallInvocation invocation) { if (!isInit) { isInit = true; var m = invocation.MethodBase; var type = m.DeclaringType; var timeAttr = m.GetCustomAttribute <PerformanceTimeAttribute>(true); if (!string.IsNullOrEmpty(timeAttr.Method)) { var bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; var types = new Type[] { typeof(PerformanceTimeContext) }; callbackMethod = type.GetMethod(timeAttr.Method, types, bindingFlags); } } PerformanceTimeContext ctx = new PerformanceTimeContext() { Target = invocation.Target, Method = invocation.MethodBase, StartTime = DateTime.Now, }; invocation.Data[key] = ctx; }
private ICallReturn RemoveEventSubscription(ICallInvocation invocation) { var subscriber = (PropertyChangedEventHandler)invocation.Arguments[0]; propertyChanged -= subscriber; return(invocation.Return(null)); }
public void BeforeInvoke(ICallInvocation invocation) { var operNames = operationNames; var providerNames = this.permissionProviderNames; IPermissionProvider permissionProvider; if (operNames.Length > 0) { for (int i = 0, len = operNames.Length; i < len; i++) { var operName = operNames[i]; var providerName = providerNames[i]; if (!invocation.TryGetValue <IPermissionProvider>(providerName, out permissionProvider)) { throw new PermissionException(operName, "not found <IPermissionProvider>, provider name: {0}".FormatArgs(providerName)); } if (!permissionProvider.HasPermission(operName)) { throw new PermissionInvalidOperationException(operName); } } } }
public void AfterInvoke(ICallInvocation invocation) { var ctx = (PerformanceTimeContext)invocation.Data[key]; ctx.EndTime = DateTime.Now; if (callbackMethod != null) { callbackMethod.Invoke(callbackMethod.IsStatic ? null : invocation.Target, new object[] { ctx }); } }
public static bool TryGetValue <T>(this ICallInvocation source, string name, out T value) { object o; if (source.TryGetValue(typeof(T), name, out o)) { value = (T)o; return(true); } value = default(T); return(false); }
private ICallReturn InterceptPropertySet(ICallInvocation invocation, GetNextCallHandlerDelegate getNext) { var propertyName = invocation.MethodBase.Name.Substring(4); var returnValue = getNext()(invocation, getNext); var subscribers = propertyChanged; if (subscribers != null) { subscribers(invocation.Target, new PropertyChangedEventArgs(propertyName)); } return(returnValue); }
public ICallReturn Invoke(ICallInvocation invocation, GetNextCallHandlerDelegate getNext) { object result; MethodBase method = invocation.MethodBase; object[] args = invocation.Arguments.ToValueArray(); try { result = method.Invoke(invocation.Target, args); } catch (TargetInvocationException ex) { return(invocation.ReturnException(ex.InnerException)); } return(invocation.Return(result)); }
public ICallReturn Invoke(ICallInvocation invocation, GetNextCallHandlerDelegate getNext) { if (invocation.MethodBase == addEventMethodInfo) { return(AddEventSubscription(invocation)); } if (invocation.MethodBase == removeEventMethodInfo) { return(RemoveEventSubscription(invocation)); } if (IsPropertySetter(invocation)) { return(InterceptPropertySet(invocation, getNext)); } return(getNext()(invocation, getNext)); }
public void BeforeInvoke(ICallInvocation invocation) { var methodBase = invocation.MethodBase; var args = invocation.Arguments; int paramIndex; object value; IParameterValidator validator; for (int i = 0, len = paramIndexs.Length; i < len; i++) { paramIndex = paramIndexs[i]; validator = validators[i]; value = args[paramIndex]; if (!validator.Validate(value)) { var p = methodBase.GetParameters()[paramIndex]; throw validator.GetException(p, value); } } }
public ICallReturn Invoke(ICallInvocation invocation, GetNextCallHandlerDelegate getNext) { if (cached == null) { cached = new Dictionary <ElementCollection, CachingResult>(); } var args = invocation.Arguments; var key = new ElementCollection(args); CachingResult cachingResult; ICallReturn result = null; if (cached.TryGetValue(key, out cachingResult)) { result = cachingResult.Result; object value; value = cachingResult.Lifetime.GetValue(); if (value != null) //if (cachingResult.Lifetime.GetValue(out value)) { result.ReturnValue = value; } else { cachingResult = null; result = null; } } if (cachingResult == null) { result = getNext()(invocation, getNext); ILifetime lifetime = GetLifetime((MethodInfo)invocation.MethodBase, invocation.Target, result.ReturnValue, cachingData.lifetimeType, cachingData.lifetimeMethodName); cachingResult = new CachingResult(result, lifetime); cached[key] = cachingResult; } return(result); }
public ICallReturn Invoke(ICallInvocation invocation, NextHandlerDelegate target) { int count = handlers == null ? 0 : handlers.Length; if (count <= 0) { return(target(invocation, null)); } int handleIndex = 0; return(handlers[0].Invoke(invocation, delegate() { handleIndex++; if (handleIndex < count) { return handlers[handleIndex].Invoke; } return target; })); }
private static bool IsPropertySetter(ICallInvocation invocation) { return(invocation.MethodBase.IsSpecialName && invocation.MethodBase.Name.StartsWith("set_")); }
public void BeforeInvoke(ICallInvocation invocation) { MethodBase method = invocation.MethodBase; StackTrace trace = new StackTrace(true); int index = -1; var frames = trace.GetFrames(); for (int i = frames.Length - 1; i >= 0; i--) { var t = frames[i]; //if (t.GetMethod().DeclaringType == thisType) //{ // index = i; // break; //} var frameMethod = t.GetMethod(); if (frameMethod.Name == method.Name && frameMethod.DeclaringType.IsAssignableFrom(method.DeclaringType)) { index = i; break; } } if (index < 0) { return; } index += 1; index += Offset; if (index >= 0 && index < frames.Length) { var frame = frames[index]; var args = invocation.Arguments; object value; foreach (var it in Parameters) { switch (it.ValueType) { case CallerParameterType.FilePath: value = frame.GetFileName(); break; case CallerParameterType.LineNumber: value = frame.GetFileLineNumber(); break; case CallerParameterType.MemberName: default: var m = frame.GetMethod(); if (m.IsSpecialName) { int index2 = m.Name.IndexOf("_"); if (index2 >= 0) { value = m.Name.Substring(index2 + 1); } else { value = m.Name; } } else { value = m.Name; } break; } args[it.ParameterIndex] = value; } } }
public void ThrowsInvoke(ICallInvocation invocation, Exception exception) { throw new NotImplementedException(); }
public void AfterInvoke(ICallInvocation invocation) { throw new NotImplementedException(); }