internal static bool IsMethodLevelAspect(this IAspect aspect) { var type = aspect.GetType(); return(typeof(OnMethodBoundaryAspectAttribute).IsAssignableFrom(type) || typeof(MethodInterceptionAspectAttribute).IsAssignableFrom(type)); }
internal static bool IsPropertyLevelAspect(this IAspect aspect) { var type = aspect.GetType(); return(typeof(PropertyInterceptionAspectAttribute).IsAssignableFrom(type) || typeof(GetPropertyInterceptionAspect).IsAssignableFrom(type) || typeof(SetPropertyInterceptionAspect).IsAssignableFrom(type)); }
/// <summary> /// Adds a new Warning message. /// </summary> /// <param name="aspect">The aspect writing the Warning message</param> /// <param name="code">The 'code' of this specific message</param> /// <param name="message">The message</param> /// <param name="location">The location where the aspect applies to. Will show full signature.</param> public static void RaiseWarning(this IAspect aspect, int code, string message, MethodBase location) { if (aspect == null || message == null) { return; } Message.Write(SeverityType.Warning, aspect.GetType().Name + "[" + code + "]", location != null ? "[{0}] {1}".F(location.AsSignature(true), message) : "{0}".F(message)); }
public static void ValidatePropertyAspect(IAspect aspect, PropertyInfo contractProperty, PropertyInfo implementationProperty) { MethodInfo[] overridenMethods = null; var propertyName = contractProperty.Name; if (contractProperty.Equals(implementationProperty)) { return; } if ((contractProperty.CanRead != implementationProperty.CanRead || contractProperty.CanWrite != implementationProperty.CanWrite) && implementationProperty.DeclaringType.IsInterface) { var contractDeclaringType = contractProperty.DeclaringType; var implementationDeclaringType = contractProperty.DeclaringType; throw new PropertyAccessorsMismatchException(CoreResources.PropertiesAccessorsMismatach.Fmt(propertyName, contractDeclaringType.FullName, implementationDeclaringType.FullName)); } if (!typeof(IPropertyInterceptionAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.PropertyInterceptionAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } overridenMethods = aspect.AspectType.GetOverridenMethods().ToArray(method => method.Name.Equals("OnGetValue") || method.Name.Equals("OnSetValue")); if (overridenMethods.Length == 0) { throw new AdviceNotFoundException(aspect.GetType()); } overridenMethods.ForEach(overridenMethod => { Type argumentsType = null; Type[] genericArguments = null; var aspectParameters = overridenMethod.GetParameters(); var aspectMethodIsFunction = overridenMethod.IsFunction(); if (aspectParameters.Length != 1 || aspectMethodIsFunction) { throw new AspectTypeMismatchException(Resources.AspectPropertyParameterMismatach.Fmt(propertyName)); } argumentsType = aspectParameters[0].ParameterType; genericArguments = argumentsType.GetGenericArguments(); if (!ValidateTypesAreEqual(contractProperty.PropertyType, genericArguments.FirstOrDefault())) { throw new AspectTypeMismatchException(Resources.AspectPropertyParameterMismatach.Fmt(propertyName)); } }); }
internal static ExecutionArgs GetExistsExecutionArgs(this List <ExecutionItem> executions, IAspect aspect) { if (executions == null) { return(null); } else { return(executions.FirstOrDefault(x => x.Aspect.GetType().GUID == aspect.GetType().GUID)?.ExecutionArgs); } }
/// <summary> /// Adds a new Error message. /// </summary> /// <param name="aspect">The aspect writing the Error message</param> /// <param name="code">The 'code' of this specific message</param> /// <param name="message">The message</param> /// <param name="location">Where was the aspect applied on, can be null</param> /// <returns>Always returns false</returns> public static bool RaiseError(this IAspect aspect, int code, string message, string location) { if (aspect == null || message == null) { return(false); } Message.Write(SeverityType.Error, aspect.GetType().Name + "[" + code + "]", location != null ? "[{0}] {1}".F(location, message) : "{0}".F(message)); return(false); }
/// <summary> /// Add weavers that target satisfies the target object. /// </summary> /// <param name="aspect"></param> public void AddAspect(IAspect aspect) { if (aspect != null) { lock (new object()) { if (chain.FirstOrDefault(c => aspect.GetType().IsAssignableFrom(c.GetType())) == null) { chain.AddLast(aspect); } } } }
private static object CallInterceptorChina(IEnumerable <IAspect> aspects, AspectMetadata lastest) { var enumerator = aspects.GetEnumerator(); AspectMetadata metadata = lastest; IAspect first = !enumerator.MoveNext() ? new DefaultAspectAttribute() : enumerator.Current; while (enumerator.MoveNext()) { IAspect current = enumerator.Current; Type aspectType = current.GetType(); Type[] types = new Type[] { typeof(AspectMetadata) }; MethodInfo method = aspectType.GetMethod("CallInterecptor", types); metadata = CreateAspectMetadata(method, current, types, new object[] { metadata }); } return(first.CallInterecptor(metadata)); }
public static void ValidateEventAspect(IAspect aspect, EventInfo @event) { var comparedTypes = Type.EmptyTypes; var invokeMethod = @event.GetInvokeMethod(); var methodIsFunction = invokeMethod.IsFunction(); var methodParameters = invokeMethod.GetParameters(); var overridenMethods = aspect.AspectType .GetOverridenMethods() .ToArray(overridenMethod => { return(overridenMethod.Name.Equals("OnAddHandler") || overridenMethod.Name.Equals("OnInvokeHandler") || overridenMethod.Name.Equals("OnRemoveHandler")); }); if (!typeof(IEventInterceptionAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.EventInterceptionAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } if (overridenMethods.Length == 0) { throw new AdviceNotFoundException(aspect.GetType()); } overridenMethods.ForEach(overridenMethod => { Type argumentsType = null; var eventName = @event.Name; Type[] genericArguments = null; var aspectParameters = overridenMethod.GetParameters(); var aspectMethodIsFunction = overridenMethod.IsFunction(); if (aspectParameters.Length != 1 || aspectMethodIsFunction) { throw new AspectTypeMismatchException(Resources.AspectEventParametersMismatach.Fmt(eventName)); } argumentsType = aspectParameters[0].ParameterType; genericArguments = argumentsType.GetGenericArguments(); if (methodIsFunction) { var argumentsLength = 0; Type aspectReturnType = null; if (typeof(IEventActionInterceptionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.EventActionInterceptionAspcetMismatch); } if (genericArguments.Length == 0) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(eventName)); } argumentsLength = genericArguments.Length - 1; aspectReturnType = genericArguments[argumentsLength]; if (genericArguments.Length > 1) { comparedTypes = genericArguments.Take(argumentsLength) .ToArray(); } if (!ValidateTypesAreEqual(invokeMethod.ReturnType, aspectReturnType)) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(eventName)); } } else { comparedTypes = genericArguments; if (typeof(IEventFunctionInterceptionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.EventActionInterceptionAspcetMismatch); } } if (!ValidateParameters(methodParameters, comparedTypes)) { throw new AspectTypeMismatchException(Resources.AspectEventParametersMismatach.Fmt(eventName)); } }); }
public static void ValidateMethodAspect(IAspect aspect, MethodInfo methodInfo) { MethodInfo method = null; Type argumentsType = null; Type[] genericArguments = null; Type[] comparedTypes = Type.EmptyTypes; ParameterInfo[] methodParameters = null; ParameterInfo[] aspectParameters = null; var overridenMethods = aspect.AspectType.GetOverridenMethods(); if (overridenMethods.Length == 0) { throw new AdviceNotFoundException(aspect.GetType()); } if (aspect.Is <OnMethodBoundaryAspectAttribute>() && !typeof(IOnMethodBoundaryAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.OnMethodBoundaryAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } if (aspect.Is <MethodInterceptionAspectAttribute>() && !typeof(IMethodInterceptionAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.MethodInterceptionAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } method = overridenMethods[0]; aspectParameters = method.GetParameters(); if (aspectParameters.Length == 0) { throw new AspectTypeMismatchException(Resources.AspectParametersMismatach.Fmt(methodInfo.Name)); } methodParameters = methodInfo.GetParameters(); argumentsType = aspectParameters[0].ParameterType; genericArguments = argumentsType.GetGenericArguments(); if (methodInfo.HasReturnType()) { int argumentsLength = 0; Type aspectReturnType = null; if (typeof(IActionExecutionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.FunctionAspectMismatch); } if (genericArguments.Length == 0) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(methodInfo.Name)); } argumentsLength = genericArguments.Length - 1; aspectReturnType = genericArguments[argumentsLength]; if (genericArguments.Length > 1) { comparedTypes = genericArguments.Take(argumentsLength) .ToArray(); } if (!ValidateReturnType(methodInfo.ReturnType, aspectReturnType)) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(methodInfo.Name)); } } else { comparedTypes = genericArguments; if (typeof(IFunctionExecutionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.FunctionAspectMismatch); } } if (!ValidateParameters(methodParameters, comparedTypes)) { throw new AspectTypeMismatchException(Resources.AspectParametersMismatach.Fmt(methodInfo.Name)); } }
internal static bool Is <TAspect>(this IAspect aspect) where TAspect : IAspect { return(typeof(TAspect).IsAssignableFrom(aspect.GetType())); }
public static void ValidateMethodAspect(IAspect aspect, MethodInfo method) { var methodName = method.Name; var comparedTypes = Type.EmptyTypes; MethodInfo[] overridenMethods = null; var methodIsFunction = method.IsFunction(); var methodParameters = method.GetParameters(); if (aspect.Is <OnMethodBoundaryAspectAttribute>()) { if (!typeof(IOnMethodBoundaryAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.OnMethodBoundaryAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } overridenMethods = aspect.AspectType.GetOverridenMethods() .ToArray(overridenMethod => { return(overridenMethod.Name.Equals("OnExit") || overridenMethod.Name.Equals("OnEntry") || overridenMethod.Name.Equals("OnSuccess") || overridenMethod.Name.Equals("OnException")); }); } else if (aspect.Is <MethodInterceptionAspectAttribute>()) { if (!typeof(IMethodInterceptionAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.MethodInterceptionAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } overridenMethods = aspect.AspectType.GetOverridenMethods() .ToArray(overridenMethod => overridenMethod.Name.Equals("OnInvoke")); } if (overridenMethods.Length == 0) { throw new AdviceNotFoundException(aspect.GetType()); } overridenMethods.ForEach(overridenMethod => { Type argumentsType = null; Type[] genericArguments = null; var aspectParameters = overridenMethod.GetParameters(); var aspectMethodIsFunction = overridenMethod.IsFunction(); if (aspectParameters.Length != 1 || aspectMethodIsFunction) { throw new AspectTypeMismatchException(Resources.AspectMethodParametersMismatach.Fmt(methodName)); } argumentsType = aspectParameters[0].ParameterType; genericArguments = argumentsType.GetGenericArguments(); if (methodIsFunction) { var argumentsLength = 0; Type aspectReturnType = null; if (typeof(IActionExecutionArgs).IsAssignableFrom(argumentsType) || typeof(IActionInterceptionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.OnActionBoundaryAspcetMismatch); } if (genericArguments.Length == 0) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(methodName)); } argumentsLength = genericArguments.Length - 1; aspectReturnType = genericArguments[argumentsLength]; if (genericArguments.Length > 1) { comparedTypes = genericArguments.Take(argumentsLength) .ToArray(); } if (!ValidateTypesAreEqual(method.ReturnType, aspectReturnType)) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(methodName)); } } else { comparedTypes = genericArguments; if (typeof(IFunctionExecutionArgs).IsAssignableFrom(argumentsType) || typeof(IFunctionInterceptionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.OnFunctionBoundaryAspcetMismatch); } } if (!ValidateParameters(methodParameters, comparedTypes)) { throw new AspectTypeMismatchException(Resources.AspectMethodParametersMismatach.Fmt(methodName)); } }); }
public static void ValidateEventAspect(IAspect aspect, EventInfo @event) { var comparedTypes = Type.EmptyTypes; var invokeMethod = @event.GetInvokeMethod(); var methodIsFunction = invokeMethod.IsFunction(); var methodParameters = invokeMethod.GetParameters(); var overridenMethods = aspect.AspectType .GetOverridenMethods() .ToArray(overridenMethod => { return overridenMethod.Name.Equals("OnAddHandler") || overridenMethod.Name.Equals("OnInvokeHandler") || overridenMethod.Name.Equals("OnRemoveHandler"); }); if (!typeof(IEventInterceptionAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.EventInterceptionAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } if (overridenMethods.Length == 0) { throw new AdviceNotFoundException(aspect.GetType()); } overridenMethods.ForEach(overridenMethod => { Type argumentsType = null; var eventName = @event.Name; Type[] genericArguments = null; var aspectParameters = overridenMethod.GetParameters(); var aspectMethodIsFunction = overridenMethod.IsFunction(); if (aspectParameters.Length != 1 || aspectMethodIsFunction) { throw new AspectTypeMismatchException(Resources.AspectEventParametersMismatach.Fmt(eventName)); } argumentsType = aspectParameters[0].ParameterType; genericArguments = argumentsType.GetGenericArguments(); if (methodIsFunction) { var argumentsLength = 0; Type aspectReturnType = null; if (typeof(IEventActionInterceptionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.EventActionInterceptionAspcetMismatch); } if (genericArguments.Length == 0) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(eventName)); } argumentsLength = genericArguments.Length - 1; aspectReturnType = genericArguments[argumentsLength]; if (genericArguments.Length > 1) { comparedTypes = genericArguments.Take(argumentsLength) .ToArray(); } if (!ValidateTypesAreEqual(invokeMethod.ReturnType, aspectReturnType)) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(eventName)); } } else { comparedTypes = genericArguments; if (typeof(IEventFunctionInterceptionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.EventActionInterceptionAspcetMismatch); } } if (!ValidateParameters(methodParameters, comparedTypes)) { throw new AspectTypeMismatchException(Resources.AspectEventParametersMismatach.Fmt(eventName)); } }); }
public static void ValidateMethodAspect(IAspect aspect, MethodInfo method) { var methodName = method.Name; var comparedTypes = Type.EmptyTypes; MethodInfo[] overridenMethods = null; var methodIsFunction = method.IsFunction(); var methodParameters = method.GetParameters(); if (aspect.Is<OnMethodBoundaryAspectAttribute>()) { if (!typeof(IOnMethodBoundaryAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.OnMethodBoundaryAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } overridenMethods = aspect.AspectType.GetOverridenMethods() .ToArray(overridenMethod => { return overridenMethod.Name.Equals("OnExit") || overridenMethod.Name.Equals("OnEntry") || overridenMethod.Name.Equals("OnSuccess") || overridenMethod.Name.Equals("OnException"); }); } else if (aspect.Is<MethodInterceptionAspectAttribute>()) { if (!typeof(IMethodInterceptionAspect).IsAssignableFrom(aspect.AspectType)) { var argumentException = new ArgumentException(Resources.MethodInterceptionAspectAttributeErrorInitialization, "aspectType"); throw new AspectAnnotationException(argumentException); } overridenMethods = aspect.AspectType.GetOverridenMethods() .ToArray(overridenMethod => overridenMethod.Name.Equals("OnInvoke")); } if (overridenMethods.Length == 0) { throw new AdviceNotFoundException(aspect.GetType()); } overridenMethods.ForEach(overridenMethod => { Type argumentsType = null; Type[] genericArguments = null; var aspectParameters = overridenMethod.GetParameters(); var aspectMethodIsFunction = overridenMethod.IsFunction(); if (aspectParameters.Length != 1 || aspectMethodIsFunction) { throw new AspectTypeMismatchException(Resources.AspectMethodParametersMismatach.Fmt(methodName)); } argumentsType = aspectParameters[0].ParameterType; genericArguments = argumentsType.GetGenericArguments(); if (methodIsFunction) { var argumentsLength = 0; Type aspectReturnType = null; if (typeof(IActionExecutionArgs).IsAssignableFrom(argumentsType) || typeof(IActionInterceptionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.OnActionBoundaryAspcetMismatch); } if (genericArguments.Length == 0) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(methodName)); } argumentsLength = genericArguments.Length - 1; aspectReturnType = genericArguments[argumentsLength]; if (genericArguments.Length > 1) { comparedTypes = genericArguments.Take(argumentsLength) .ToArray(); } if (!ValidateTypesAreEqual(method.ReturnType, aspectReturnType)) { throw new AspectTypeMismatchException(Resources.AspectReturnTypeMismatch.Fmt(methodName)); } } else { comparedTypes = genericArguments; if (typeof(IFunctionExecutionArgs).IsAssignableFrom(argumentsType) || typeof(IFunctionInterceptionArgs).IsAssignableFrom(argumentsType)) { throw new AspectAnnotationException(Resources.OnFunctionBoundaryAspcetMismatch); } } if (!ValidateParameters(methodParameters, comparedTypes)) { throw new AspectTypeMismatchException(Resources.AspectMethodParametersMismatach.Fmt(methodName)); } }); }
public static Type[] GetAssociatedComponentTypes(this IAspect aspect) { return(GetAssociatedComponentTypes(aspect.GetType())); }
public MethodInterceptor(object service, IAspect aspect) { _service = service; _aspect = aspect; Console.WriteLine("Wiring {0} on {1}", aspect.GetType(), service.GetType()); }