Ejemplo n.º 1
0
        private static List <IValidationElement> GetValidationAttributes(MemberInfo originalMember, MemberInfo metadataMember)
        {
            var validationAttributes = new List <IValidationElement>();

            Attribute[] attributes = metadataMember.GetAttributes();
            foreach (Attribute attribute in attributes)
            {
                Type       type   = attribute.GetType();
                MethodInfo method = type
                                    .GetMethodsEx(InstancePublicFlags)
                                    .FirstOrDefault(info =>
                {
                    if (info.Name != GetValidationResultMethodName ||
                        info.ReturnType.FullName != ValidationResultTypeName)
                    {
                        return(false);
                    }
                    ParameterInfo[] parameters = info.GetParameters();
                    return(parameters.Length == 2 && parameters[0].ParameterType.Equals(typeof(object)) &&
                           parameters[1].ParameterType.FullName == ValidationContextTypeName);
                });
                if (method == null)
                {
                    continue;
                }
                ParameterInfo[] @params = method.GetParameters();

                var validationContextConverter = TryGetValidationContextConverter(@params[1].ParameterType);
                if (validationContextConverter == null)
                {
                    continue;
                }
                Func <object, object[], object> methodDelegate   = ServiceProvider.ReflectionManager.GetMethodDelegate(method);
                Func <object, object>           getPropertyValue = ServiceProvider.ReflectionManager.GetMemberGetter <object>(originalMember);
                Attribute     attr = attribute;
                Func <string> displayNameAccessor = GetDisplayNameAccessor(originalMember);
                var           element             = new DynamicValidationAttribute(originalMember.Name, displayNameAccessor, getPropertyValue,
                                                                                   (o, context) => methodDelegate(attr, new[] { o, validationContextConverter(context) }));
                if (Tracer.TraceInformation)
                {
                    Tracer.Info("Added a '{0}' validation attribute for type: '{1}', member: '{2}'", attr,
                                originalMember.DeclaringType, originalMember);
                }
                validationAttributes.Add(element);
            }
            return(validationAttributes);
        }
Ejemplo n.º 2
0
 public DynamicModelValidatorProvider(DynamicValidationAttribute dynamicValidationAttribute)
 {
     this.dynamicValidationAttribute = dynamicValidationAttribute;
 }