Esempio n. 1
0
        /// <summary>
        /// Gets the properties of the specified type.
        /// </summary>
        protected virtual IEnumerable <ViewModelPropertyMap> GetProperties(Type type)
        {
            foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(p => p.Name))
            {
                if (property.GetCustomAttribute <JsonIgnoreAttribute>() != null)
                {
                    continue;
                }

                var propertyMap = new ViewModelPropertyMap()
                {
                    PropertyInfo          = property,
                    Name                  = property.Name,
                    ViewModelProtection   = ProtectMode.None,
                    Type                  = property.PropertyType,
                    TransferAfterPostback = property.GetMethod != null && property.GetMethod.IsPublic,
                    TransferFirstRequest  = property.GetMethod != null && property.GetMethod.IsPublic,
                    TransferToServer      = property.SetMethod != null && property.SetMethod.IsPublic,
                    JsonConverter         = GetJsonConverter(property),
                    Populate              = ViewModelJsonConverter.IsComplexType(property.PropertyType) && !ViewModelJsonConverter.IsEnumerable(property.PropertyType) && property.GetMethod != null
                };

                foreach (ISerializationInfoAttribute attr in property.GetCustomAttributes().OfType <ISerializationInfoAttribute>())
                {
                    attr.SetOptions(propertyMap);
                }

                var bindAttribute = property.GetCustomAttribute <BindAttribute>();
                if (bindAttribute != null)
                {
                    propertyMap.Bind(bindAttribute.Direction);
                    if (bindAttribute.Name != null)
                    {
                        propertyMap.Name = bindAttribute.Name;
                    }
                }

                var viewModelProtectionAttribute = property.GetCustomAttribute <ProtectAttribute>();
                if (viewModelProtectionAttribute != null)
                {
                    propertyMap.ViewModelProtection = viewModelProtectionAttribute.Settings;
                }

                propertyMap.ClientExtenders =
                    property.GetCustomAttributes <ClientExtenderAttribute>()
                    .OrderBy(c => c.Order)
                    .Select(extender => new ClientExtenderInfo()
                {
                    Name = extender.Name, Parameter = extender.Parameter
                })
                    .ToList();

                var validationAttributes = validationMetadataProvider.GetAttributesForProperty(property);
                propertyMap.ValidationRules = validationRuleTranslator.TranslateValidationRules(property, validationAttributes).ToList();

                propertyMap.ValidateSettings();

                yield return(propertyMap);
            }
        }
Esempio n. 2
0
 public static void Ignore(this ViewModelPropertyMap property)
 {
     property.Bind(Direction.None);
     property.ValidationRules.Clear();
     property.ClientExtenders.Clear();
 }