Пример #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
                };

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

                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();

                yield return(propertyMap);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the properties of the specified type.
        /// </summary>
        private 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   = ViewModelProtectionSettings.None,
                    Type                  = property.PropertyType,
                    TransferAfterPostback = property.GetMethod != null,
                    TransferFirstRequest  = property.GetMethod != null,
                    TransferToServer      = property.SetMethod != null,
                    JsonConverter         = GetJsonConverter(property),
                    Populate              = ViewModelJsonConverter.IsComplexType(property.PropertyType) && !ViewModelJsonConverter.IsEnumerable(property.PropertyType)
                };

                var bindAttribute = property.GetCustomAttribute <BindAttribute>();
                if (bindAttribute != null)
                {
                    propertyMap.TransferAfterPostback      = bindAttribute.Direction.HasFlag(Direction.ServerToClientPostback);
                    propertyMap.TransferFirstRequest       = bindAttribute.Direction.HasFlag(Direction.ServerToClientFirstRequest);
                    propertyMap.TransferToServer           = bindAttribute.Direction.HasFlag(Direction.ClientToServerNotInPostbackPath) || bindAttribute.Direction.HasFlag(Direction.ClientToServerInPostbackPath);
                    propertyMap.TransferToServerOnlyInPath = !bindAttribute.Direction.HasFlag(Direction.ClientToServerNotInPostbackPath) && propertyMap.TransferToServer;
                }

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

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

                yield return(propertyMap);
            }
        }