private void LocalizeDisplayAttribute(ModelMetadata metadata, DisplayAttribute attribute)
 {
     metadata.DisplayName = Localize(attribute.GetName());
     metadata.ShortDisplayName = Localize(attribute.GetShortName());
     metadata.Description = Localize(attribute.GetDescription());
     metadata.Watermark = Localize(attribute.GetPrompt());
 }
        // <summary>
        // Creates an instance of <see cref="ValidationAttributeValidator" /> class.
        // </summary>
        // <param name="validationAttribute"> Validation attribute used to validate a property or an entity. </param>
        public ValidationAttributeValidator(ValidationAttribute validationAttribute, DisplayAttribute displayAttribute)
        {
            DebugCheck.NotNull(validationAttribute);

            _validationAttribute = validationAttribute;
            _displayAttribute = displayAttribute;
        }
Esempio n. 3
0
        // : this(owner, category.GetGroupName())  //!!! dmh - switch to displayAttribute & use GetGroupName so it will use resource string if present
        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryItem"/> class.
        /// </summary>
        /// <param name="owner">The owner.</param>
        /// <param name="category">The category.</param>
        public CategoryItem(PropertyGrid owner, DisplayAttribute category)
        {
            Owner = owner;
            if (!string.IsNullOrEmpty(category.GroupName))
            {
                if (category.ResourceType != null)
                {
                    try		{ Name = category.GetGroupName(); }
                    catch	{ Name = category.GroupName; }
                }
                else Name = category.GroupName;
            }
            Attribute = category;

            // !!! dmh - be sure to get order from the display attribute for category as well.
            /* do not use order from display attribute for category! use CategoryOrderAttribute
            try
            {
                int? orderAttempt = category.GetOrder();
                if (orderAttempt.HasValue)
                    order = orderAttempt.Value;
            }
            catch { }
            */
        }
        /// <summary>
        ///     Creates an instance of <see cref = "ValidationAttributeValidator" /> class.
        /// </summary>
        /// <param name = "validationAttribute">
        ///     Validation attribute used to validate a property or an entity.
        /// </param>
        public ValidationAttributeValidator(ValidationAttribute validationAttribute, DisplayAttribute displayAttribute)
        {
            //Contract.Requires(validationAttribute != null);

            _validationAttribute = validationAttribute;
            _displayAttribute = displayAttribute;
        }
 public DisplayNameLocalizedAttribute(string resourceName)
 {
     this.display = new DisplayAttribute()
     {
         ResourceType = typeof(Resources.Global),
         Name = resourceName
     };
 }
Esempio n. 6
0
 public LocalizationDisplayNameAttribute(string resourceName, Type resourceType)
 {
     display = new DisplayAttribute
     {
         ResourceType = resourceType,
         Name = resourceName
     };
 }
        public void Copy_WithDisplayAttribute_ReturnsNewInstance()
        {
            var displayAttribute = new DisplayAttribute();

            var copy = displayAttribute.Copy();

            Assert.NotSame(displayAttribute, copy);
        }
 public LocalizedDisplayNameAttribute(string resourceName)
     : base(resourceName)
 {
     _displayAttribute = new DisplayAttribute()
     {
         ResourceType = typeof(GetAttributeValue.Global),
         Name = resourceName
     };
 }
 public void Copy_WithDisplayAttribute_ReturnsNewInstance()
 {
     // arrange
     var displayAttribute = new DisplayAttribute();
     // act
     var copy = displayAttribute.Copy();
     // assert
     Assert.NotSame(displayAttribute, copy);
 }
 public LocalizationCompareAttribute(string otherProperty, string resourceName, Type resourceType)
     : base(otherProperty)
 {
     this.display = new DisplayAttribute()
       {
      ResourceType = resourceType,
      Name = resourceName
       };
       base.ErrorMessage = display.GetName();
 }
Esempio n. 11
0
		private string GetDisplayStringValue(DisplayAttribute[] a)
		{
			if (a == null || a.Length == 0) return null;
			DisplayAttribute displayAttribute = a[0];
			if (displayAttribute.ResourceType != null)
			{
				ResourceManager resourceManager = new ResourceManager(displayAttribute.ResourceType);
				return resourceManager.GetString(displayAttribute.Name);
			}

			return displayAttribute.Name;
		}
        public LocalizationStringLengthAttribute(int maxLength, int minLength, string resourceName, Type resourceType)
            : base(maxLength)
        {
            this.display = new DisplayAttribute()
              {
             ResourceType = resourceType,
             Name = resourceName
              };

              this.MinimumLength = minLength;
              this.ErrorMessage = display.GetName();
        }
        public void Copy_WithDisplayAttribute_ReturnsNewInstanceWithCopiedProperties()
        {
            var displayAttribute = new DisplayAttribute
            {
                Name = "TheName",
                Description = "TheDescription",
                ResourceType = typeof (object)
            };

            var copy = displayAttribute.Copy();

            Assert.Equal(displayAttribute.Name, copy.Name);
        }
 public static void SetDisplayName(
     this ValidationContext validationContext, InternalMemberEntry property, DisplayAttribute displayAttribute)
 {
     var displayName = displayAttribute == null ? null : displayAttribute.Name;
     if (property == null)
     {
         var objectType = ObjectContextTypeCache.GetObjectType(validationContext.ObjectType);
         validationContext.DisplayName = displayName ?? objectType.Name;
         validationContext.MemberName = null;
     }
     else
     {
         validationContext.DisplayName = displayName ?? DbHelpers.GetPropertyPath(property);
         validationContext.MemberName = DbHelpers.GetPropertyPath(property);
     }
 }
        public static DisplayAttribute Copy(this DisplayAttribute attribute)
        {
            if (attribute == null)
            {
                return null;
            }
            var copy = new DisplayAttribute();

            // DisplayAttribute is sealed, so safe to copy.
            copy.Name = attribute.Name;
            copy.GroupName = attribute.GroupName;
            copy.Description = attribute.Description;
            copy.ResourceType = attribute.ResourceType;
            copy.ShortName = attribute.ShortName;

            return copy;
        }
Esempio n. 16
0
        /// <summary>
        /// 如果没有DisplayName则返回 null
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static string GetDisplayName(System.Reflection.PropertyInfo info)
        {
            object[] _DisplayList1 = info.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), true);

            if (_DisplayList1.Length != 0)
            {
                System.ComponentModel.DataAnnotations.DisplayAttribute _Display1 = (System.ComponentModel.DataAnnotations.DisplayAttribute)_DisplayList1[0];
                return(_Display1.Name);
            }


            object[] _DisplayList = info.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true);
            if (_DisplayList.Length == 0)
            {
                return(null);
            }

            System.ComponentModel.DisplayNameAttribute _Display = (System.ComponentModel.DisplayNameAttribute)_DisplayList[0];
            return(_Display.DisplayName);
        }
        public static DisplayAttribute Copy(this DisplayAttribute attribute)
        {
            if (attribute == null)
            {
                return null;
            }

            // DisplayAttribute is sealed, so it's safe to copy.
            var copy = new DisplayAttribute
            {
                Name = attribute.Name,
                GroupName = attribute.GroupName,
                Description = attribute.Description,
                ResourceType = attribute.ResourceType,
                ShortName = attribute.ShortName,
                Prompt = attribute.Prompt
            };

            return copy;
        }
        /// <summary>
        /// 重写生成元数据的方法
        /// 只有页面上来通过Lambda表达式过来的元数据,才进行中英文转换
        /// (该过滤为了减少元数据的中英文转换次数)
        /// </summary>
        /// <param name="attributes"></param>
        /// <param name="containerType"></param>
        /// <param name="modelAccessor"></param>
        /// <param name="modelType"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) {
            if (containerType != null) {//请改之前先做好测试!这是最终的条件.

                //动态类型/代理类 EF
                var t = containerType.Assembly.IsDynamic ? containerType.BaseType : containerType;

                var v = this.GetString(t, propertyName);
                if (!string.IsNullOrWhiteSpace(v)) {
                    //这里,如果 v 是空字符串的话,居然会影响到 SmartModelBinder 的 ModelValidator.GetModelValidator
                    var dsp = new DisplayAttribute() {
                        Name = v
                    };
                    var attrs = attributes.ToList();
                    attrs.RemoveAll(a => a is DisplayAttribute);
                    attrs.Add(dsp);
                    return base.CreateMetadata(attrs, containerType, modelAccessor, modelType, propertyName);
                }
            }

            return base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        }
Esempio n. 19
0
        public static DisplayAttribute Copy(this DisplayAttribute srcAttribute)
        {
            if (srcAttribute == null)
            {
                return null;
            }

            var copy = new DisplayAttribute
                {
                    Name = srcAttribute.Name,
                    GroupName = srcAttribute.GroupName,
                    Description = srcAttribute.Description,
                    ResourceType = srcAttribute.ResourceType,
                    ShortName = srcAttribute.ShortName,
                    Prompt = srcAttribute.Prompt
                };

            var order = srcAttribute.GetOrder();
            if (order != null)
            {
                copy.Order = order.Value;
            }

            var autoGenerateField = srcAttribute.GetAutoGenerateField();
            if (autoGenerateField != null)
            {
                copy.AutoGenerateField = autoGenerateField.Value;
            }

            var autoGenerateFilter = srcAttribute.GetAutoGenerateFilter();
            if (autoGenerateFilter != null)
            {
                copy.AutoGenerateFilter = autoGenerateFilter.Value;
            }

            return copy;
        }
Esempio n. 20
0
        public void DisplayAttribute_PromptAsPlaceholder()
        {
            // Arrange
            var display = new DisplayAttribute() { Prompt = "prompt" };
            var provider = CreateProvider(new[] { display });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.Placeholder;

            // Assert
            Assert.Equal("prompt", result);
        }
        private static string GetDisplayAttributeName(Type containerType, string propertyName, DisplayAttribute displayAttribute)
        {
            if (containerType != null)
            {
                if (String.IsNullOrEmpty(displayAttribute.Name))
                {
                    // check to see that resource key exists.
                    string resourceKey = GetResourceKey(containerType, propertyName);
                    if (displayAttribute.ResourceType.PropertyExists(resourceKey))
                    {
                        return resourceKey;
                    }
                    else
                    {
                        return propertyName;
                    }
                }

            }
            return null;
        }
        public void GetDisplayMetadata_DisplayAttribute_DescriptionFromResources()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var display = new DisplayAttribute()
            {
#if USE_REAL_RESOURCES
                Description = nameof(Test.Resources.DisplayAttribute_Description),
                ResourceType = typeof(Test.Resources),
#else
                Description = nameof(DataAnnotations.Test.Resources.DisplayAttribute_Description),
                ResourceType = typeof(TestResources),
#endif
            };

            var attributes = new Attribute[] { display };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal("description from resources", context.DisplayMetadata.Description());
        }
        public void GetDisplayDetails_DisplayAttribute_NameFromResources()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var display = new DisplayAttribute()
            {
#if USE_REAL_RESOURCES
                Name = nameof(Test.Resources.DisplayAttribute_Name),
                ResourceType = typeof(Test.Resources),
#else
                Name = nameof(Test.Resources.DisplayAttribute_Name),
                ResourceType = typeof(Test.TestResources),
#endif
            };

            var attributes = new Attribute[] { display };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, attributes);

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Equal("name from resources", context.DisplayMetadata.DisplayName);
        }
 /// <summary>
 /// Gets the text field vm.
 /// </summary>
 /// <param name="prop">The property.</param>
 /// <param name="model">The model.</param>
 /// <param name="textAttr">The text attribute.</param>
 /// <param name="promptAttr">The prompt attribute.</param>
 /// <param name="display">The display.</param>
 /// <param name="fieldEditorAttr">The field editor attribute.</param>
 /// <returns></returns>
 private TextFieldViewModel GetTextFieldVM(PropertyInfo prop, IEditableRoot model, TextFieldAttribute textAttr, PromptAttribute promptAttr, DisplayAttribute display, FieldEditorAttribute fieldEditorAttr)
 {
     var vm = TextFieldViewModelFactory.CreateExport().Value;
     vm.NumberOfRows = textAttr.NumberOfRows;
     vm.Mask = textAttr.Mask;
     vm.MaskType = textAttr.MaskType;
     vm.MaxLength = textAttr.NumberOfCharacters;
     SetupField(prop, promptAttr, display, vm, model);
     vm.FieldType = GetFieldEditor(prop, fieldEditorAttr);
     return vm;
 }
        public void DisplayAttribute_Description()
        {
            // Arrange
            var display = new DisplayAttribute() { Description = "description" };
            var provider = CreateProvider(new[] { display });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.Description;

            // Assert
            Assert.Equal("description", result);
        }
 public ValidatableObjectValidator(DisplayAttribute displayAttribute)
 {
     _displayAttribute = displayAttribute;
 }
 /// <summary>
 /// Setups the field.
 /// </summary>
 /// <param name="prop">The property.</param>
 /// <param name="promptAttr">The prompt attribute.</param>
 /// <param name="display">The display.</param>
 /// <param name="fieldViewModel">The field view model.</param>
 /// <param name="model">The model.</param>
 /// <param name="defaultValue">The default value.</param>
 private static void SetupField(PropertyInfo prop, PromptAttribute promptAttr, DisplayAttribute display, IFieldViewModel fieldViewModel, IEditableRoot model, object defaultValue = null)
 {
     fieldViewModel.Property = prop;
     fieldViewModel.Init(model, prop.GetValue(model, null) ?? defaultValue, null);
     fieldViewModel.Label = display == null ? prop.Name : display.GetName();
     fieldViewModel.IsCalculated = prop.GetCustomAttributes(typeof(CalculatedAttribute), false).Length > 0;
     //fieldViewModel.WidthPercentage = 1.0; // (commonSettingsAttr == null || commonSettingsAttr.HideFromDetails) ? 0.4 : commonSettingsAttr.Width / 100;
     fieldViewModel.IsVisible = true;
     fieldViewModel.Prompt = promptAttr == null ? string.Format(CultureInfo.InvariantCulture, LanguageService.Translate("Tooltip_EnterPrompt"), fieldViewModel.Label) : promptAttr.PromptString;
     //fieldViewModel.ShowDocumentation = docAttr != null;
     //fieldViewModel.Documentation = docAttr == null ? null : ClrUnicodeConverter.ToText(docAttr.DocumentationBody);
 }
        public void DisplayAttribute_OverridesOrder(DisplayAttribute attribute, int expectedOrder)
        {
            // Arrange
            var attributes = new[] { attribute };
            var provider = CreateProvider(attributes);

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.Order;

            // Assert
            Assert.Equal(expectedOrder, result);
        }
Esempio n. 29
0
 public JsonReferenceProperty(ModelType declaringType, PropertyInfo property, string name, string label, string helptext, string format, bool isStatic, ModelType propertyType, bool isList, bool isReadOnly, bool isPersisted, Attribute[] attributes)
     : base(declaringType, property, name, label, helptext, format, isStatic, propertyType, isList, isReadOnly, isPersisted, attributes)
 {
     displayAttribute = GetAttributes<DisplayAttribute>().FirstOrDefault();
 }
        public void Description_FromResources_GetsRecomputed()
        {
            // Arrange
            var display = new DisplayAttribute()
            {
                Description = nameof(TestResources.DisplayAttribute_CultureSensitiveDescription),
                ResourceType = typeof(TestResources),
            };

            var provider = CreateProvider(new[] { display });
            var metadata = provider.GetMetadataForType(typeof(string));

            // Act & Assert
            var cultures = new[] { "fr-FR", "en-US", "en-GB" };
            foreach (var culture in cultures)
            {
                using (new CultureReplacer(uiCulture: culture))
                {
                    // Later iterations ensure value is recomputed.
                    var result = metadata.Description;
                    Assert.Equal("description from resources" + culture, result);
                }
            }
        }
Esempio n. 31
0
 public JsonValueProperty(ModelType declaringType, PropertyInfo property, string name, string label, string helptext, bool isStatic, Type propertyType, TypeConverter converter, bool isList, bool isReadOnly, bool isPersisted, Attribute[] attributes, LambdaExpression defaultValue = null)
     : base(declaringType, property, name, label, helptext, GetFormat(propertyType, attributes), isStatic, propertyType, converter, isList, isReadOnly, isPersisted, attributes, defaultValue)
 {
     displayAttribute = GetAttributes<DisplayAttribute>().FirstOrDefault();
 }