NCop.Aspects.Engine.AspectTypeValidator.ValidatePropertyAspect C# (CSharp) Method

ValidatePropertyAspect() public static method

public static ValidatePropertyAspect ( IAspect aspect, PropertyInfo contractProperty, PropertyInfo implementationProperty ) : void
aspect IAspect
contractProperty System.Reflection.PropertyInfo
implementationProperty System.Reflection.PropertyInfo
return void
        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));
                }
            });
        }

Same methods

AspectTypeValidator::ValidatePropertyAspect ( PropertyInfo target, IAspect aspect, AspectMap aspectMap ) : void

Usage Example

 public void Visit(PropertyInfo property, IAspect aspect, AspectMap aspectMap)
 {
     AspectTypeValidator.ValidatePropertyAspect(property, aspect, aspectMap);
 }