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)); } }
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 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)); } }); }
internal static bool IsNot <TAspect>(this IAspect aspect) { return(!aspect.Is <TAspect>()); }