Пример #1
0
        public void DisplayAttribute_Resourced_Properties()
        {
            DisplayAttribute attr = new DisplayAttribute();

            attr.ResourceType = typeof(DisplayAttribute_Resources);

            Assert.IsNull(attr.GetShortName());
            Assert.IsNull(attr.GetName());
            Assert.IsNull(attr.GetPrompt());
            Assert.IsNull(attr.GetDescription());
            Assert.IsNull(attr.GetGroupName());

            attr.ShortName   = "Resource1";
            attr.Name        = "Resource2";
            attr.Prompt      = "Resource3";
            attr.Description = "Resource4";
            attr.GroupName   = "Resource5";

            Assert.AreEqual("string1", attr.GetShortName());
            Assert.AreEqual("string2", attr.GetName());
            Assert.AreEqual("string3", attr.GetPrompt());
            Assert.AreEqual("string4", attr.GetDescription());
            Assert.AreEqual("string5", attr.GetGroupName());

            Assert.AreEqual("Resource1", attr.ShortName);
            Assert.AreEqual("Resource2", attr.Name);
            Assert.AreEqual("Resource3", attr.Prompt);
            Assert.AreEqual("Resource4", attr.Description);
            Assert.AreEqual("Resource5", attr.GroupName);
        }
        public void DisplayAttribute_Resourced_Properties() {
            DisplayAttribute attr = new DisplayAttribute();

            attr.ResourceType = typeof(DisplayAttribute_Resources);

            Assert.IsNull(attr.GetShortName());
            Assert.IsNull(attr.GetName());
            Assert.IsNull(attr.GetPrompt());
            Assert.IsNull(attr.GetDescription());
            Assert.IsNull(attr.GetGroupName());

            attr.ShortName = "Resource1";
            attr.Name = "Resource2";
            attr.Prompt = "Resource3";
            attr.Description = "Resource4";
            attr.GroupName = "Resource5";

            Assert.AreEqual("string1", attr.GetShortName());
            Assert.AreEqual("string2", attr.GetName());
            Assert.AreEqual("string3", attr.GetPrompt());
            Assert.AreEqual("string4", attr.GetDescription());
            Assert.AreEqual("string5", attr.GetGroupName());

            Assert.AreEqual("Resource1", attr.ShortName);
            Assert.AreEqual("Resource2", attr.Name);
            Assert.AreEqual("Resource3", attr.Prompt);
            Assert.AreEqual("Resource4", attr.Description);
            Assert.AreEqual("Resource5", attr.GroupName);
        }
Пример #3
0
        public void DisplayAttribute_Reflection_Test()
        {
            Type t = typeof(DisplayAttribute_Sample);

            FieldInfo fInfo = t.GetField("stringField");

            Assert.IsNotNull(fInfo);
            Assert.IsNotNull(fInfo.GetCustomAttributes(true).OfType <DisplayAttribute>().SingleOrDefault());

            PropertyInfo pInfo = t.GetProperty("LiteralStringProperty");

            Assert.IsNotNull(pInfo);
            DisplayAttribute attr = pInfo.GetCustomAttributes(true).OfType <DisplayAttribute>().SingleOrDefault();

            Assert.IsNotNull(attr);

            Assert.IsNull(attr.ResourceType);

            Assert.AreEqual("theShortName", attr.ShortName);
            Assert.AreEqual("theName", attr.Name);
            Assert.AreEqual("thePrompt", attr.Prompt);
            Assert.AreEqual("theDescription", attr.Description);
            Assert.AreEqual("theGroupName", attr.GroupName);


            Assert.AreEqual("theShortName", attr.GetShortName());
            Assert.AreEqual("theName", attr.GetName());
            Assert.AreEqual("thePrompt", attr.GetPrompt());
            Assert.AreEqual("theDescription", attr.GetDescription());
            Assert.AreEqual("theGroupName", attr.GetGroupName());


            pInfo = t.GetProperty("ResourcedStringProperty");
            Assert.IsNotNull(pInfo);
            attr = pInfo.GetCustomAttributes(true).OfType <DisplayAttribute>().SingleOrDefault();
            Assert.IsNotNull(attr);

            Assert.AreEqual(typeof(DisplayAttribute_Resources), attr.ResourceType);

            Assert.AreEqual("string1", attr.GetShortName());
            Assert.AreEqual("Resource1", attr.ShortName);

            Assert.AreEqual("string2", attr.GetName());
            Assert.AreEqual("Resource2", attr.Name);

            Assert.AreEqual("string3", attr.GetPrompt());
            Assert.AreEqual("Resource3", attr.Prompt);

            Assert.AreEqual("string4", attr.GetDescription());
            Assert.AreEqual("Resource4", attr.Description);

            Assert.AreEqual("string5", attr.GetGroupName());
            Assert.AreEqual("Resource5", attr.GroupName);
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="obj"> </param>
        /// <param name="propertyName"> </param>
        /// <returns> </returns>
        public static string GetPropertyDescription <T>(this T obj, string propertyName) where T : NotifyPropertyBase
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return(string.Empty);
            }
            Type         tp    = obj.GetType();
            PropertyInfo pi    = tp.GetProperty(propertyName);
            var          value = pi.GetValue(obj, null);

            object[] Attributes     = pi.GetCustomAttributes(false);
            string   strDescription = "";

            if (Attributes != null && Attributes.Length > 0)
            {
                foreach (object attribute in Attributes)
                {
                    if (attribute is DisplayAttribute)
                    {
                        try
                        {
                            DisplayAttribute vAttribute = attribute as DisplayAttribute;
                            strDescription = vAttribute.GetDescription();
                            break;
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                        }
                    }
                }
            }
            return(strDescription);
        }
Пример #5
0
 public VxSelectItem(DisplayAttribute displayAttribute, Enum value)
 {
     this.Order       = displayAttribute.GetOrder() ?? 0;
     this.Label       = displayAttribute.GetName();
     this.Key         = value.ToString();
     this.Description = displayAttribute.GetDescription();
 }
 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());
 }
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            ModelMetadata res = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

            FormatAttribute formatAttribute = attributes.OfType <FormatAttribute>().FirstOrDefault();

            if (formatAttribute != null)
            {
                string clientFormat;
                string prefix;
                string postfix;
                formatAttribute.GetClientFormat(out prefix, out postfix, out clientFormat);
                res.AdditionalValues.Add("MVCControlsToolkit.ClientFormatString", clientFormat);
                res.AdditionalValues.Add("MVCControlsToolkit.ClientFormatPrefix", prefix);
                res.AdditionalValues.Add("MVCControlsToolkit.ClientFormatPostfix", postfix);
            }
            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();
            string           name    = null;

            if (display != null)
            {
                res.Description      = display.GetDescription();
                res.ShortDisplayName = display.GetShortName();
                res.Watermark        = display.GetPrompt();


                name = display.GetName();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    res.DisplayName = name;
                }
            }
            return(res);
        }
 internal static ValidationMetadata ParseMetadata(string bindingPath, object entity)
 {
     if ((entity != null) && !string.IsNullOrEmpty(bindingPath))
     {
         PropertyInfo property = GetProperty(GetCustomOrCLRType(entity), bindingPath);
         if (property != null)
         {
             ValidationMetadata metadata = new ValidationMetadata();
             foreach (object obj2 in property.GetCustomAttributes(false))
             {
                 RequiredAttribute attribute = obj2 as RequiredAttribute;
                 if (attribute != null)
                 {
                     metadata.IsRequired = true;
                 }
                 else
                 {
                     DisplayAttribute attribute2 = obj2 as DisplayAttribute;
                     if (attribute2 != null)
                     {
                         metadata.Description = attribute2.GetDescription();
                         metadata.Caption     = attribute2.GetName();
                     }
                 }
             }
             if (metadata.Caption == null)
             {
                 metadata.Caption = property.Name;
             }
             return(metadata);
         }
     }
     return(null);
 }
Пример #9
0
        public static List <EnumEntry <TEnum, TRaw> > ConvertEnumToEntryList <TEnum, TRaw>(IEnumerable <TEnum> excludes = null, Func <TRaw, string, string> func = null)
        {
            var enumType = typeof(TEnum);
            var list     = new List <EnumEntry <TEnum, TRaw> >();

            if (enumType.IsEnum == false)
            {
                return(list);
            }

            //
            TEnum[] aryEnum = Enum.GetValues(enumType) as TEnum[];
            if (aryEnum == null)
            {
                return(list);
            }

            var lstEnums = aryEnum;

            if (excludes != null)
            {
                lstEnums = aryEnum.Except(excludes).ToArray();
            }
            //
            foreach (var item in lstEnums)
            {
                string strEnum = item.ToString();
                TRaw   rawVal  = (TRaw)Convert.ChangeType(item, typeof(TRaw));
                Enum   objEnum = Enum.Parse(enumType, strEnum) as Enum;
                var    entry   = new EnumEntry <TEnum, TRaw>(item, rawVal, strEnum)
                {
                    Description = GetEnumDescription(objEnum)
                };
                //entry.DisplayName = GetEnumDisplay(objEnum);
                //entry.DisplayDesc = GetEnumDisplay(objEnum, 1);
                var disObj = GetEnumDisplayAttributs(objEnum);
                DisplayAttribute displayAttr = null;
                if (disObj != null)
                {
                    displayAttr = disObj.FirstOrDefault();
                }
                if (displayAttr != null)
                {
                    entry.DisplayName      = displayAttr.GetName();
                    entry.DisplayShortName = displayAttr.GetShortName();
                    entry.DisplayDesc      = displayAttr.GetDescription();
                    entry.DisplayOrder     = displayAttr.GetOrder() ?? 0;
                }
                if (func != null)
                {
                    entry.CustomName = func(rawVal, "");
                }

                list.Add(entry);
            }

            list = list.OrderBy(l => l.RawValue).ToList();
            return(list);
        }
Пример #10
0
        public void DisplayAttribute_GetDescription_Does_Not_Fall_Back()
        {
            DisplayAttribute attr = new DisplayAttribute {
                ShortName = "ShortName", Name = "Name", Prompt = "Prompt", GroupName = "GroupName"
            };

            Assert.IsNull(attr.GetDescription(), "Description should NOT fall back onto any other values");
        }
Пример #11
0
        public static string GetDescription(Enum value)
        {
            DisplayAttribute attribute = value.GetType()
                                         .GetField(value.ToString())
                                         .GetCustomAttributes(typeof(DisplayAttribute), false)
                                         .SingleOrDefault() as DisplayAttribute;

            return(attribute == null?value.ToString() : attribute.GetDescription());
        }
Пример #12
0
        public string GetDescription()
        {
            var localizedString = GetLocalizedString(_displayAttribute.Description);

            if (localizedString == null)
            {
                localizedString = _displayAttribute.GetDescription();
            }

            return(localizedString);
        }
Пример #13
0
        public void DisplayAttribute_Literal_Properties()
        {
            DisplayAttribute attr = new DisplayAttribute();

            Assert.IsNull(attr.ResourceType);
            Assert.IsNull(attr.ShortName);
            Assert.IsNull(attr.Name);
            Assert.IsNull(attr.Prompt);
            Assert.IsNull(attr.Description);
            Assert.IsNull(attr.GroupName);

            Assert.IsNull(attr.GetShortName());
            Assert.IsNull(attr.GetName());
            Assert.IsNull(attr.GetPrompt());
            Assert.IsNull(attr.GetDescription());
            Assert.IsNull(attr.GetGroupName());

            attr.ShortName   = "theShortName";
            attr.Name        = "theName";
            attr.Prompt      = "thePrompt";
            attr.Description = "theDescription";
            attr.GroupName   = "theGroupName";

            Assert.AreEqual("theShortName", attr.GetShortName());
            Assert.AreEqual("theName", attr.GetName());
            Assert.AreEqual("thePrompt", attr.GetPrompt());
            Assert.AreEqual("theDescription", attr.GetDescription());
            Assert.AreEqual("theGroupName", attr.GetGroupName());

            attr.ShortName   = String.Empty;
            attr.Name        = String.Empty;
            attr.Prompt      = String.Empty;
            attr.Description = String.Empty;
            attr.GroupName   = String.Empty;

            Assert.AreEqual(String.Empty, attr.GetShortName());
            Assert.AreEqual(String.Empty, attr.GetName());
            Assert.AreEqual(String.Empty, attr.GetPrompt());
            Assert.AreEqual(String.Empty, attr.GetDescription());
            Assert.AreEqual(String.Empty, attr.GetGroupName());
        }
        public void DisplayAttribute_Literal_Properties() {
            DisplayAttribute attr = new DisplayAttribute();

            Assert.IsNull(attr.ResourceType);
            Assert.IsNull(attr.ShortName);
            Assert.IsNull(attr.Name);
            Assert.IsNull(attr.Prompt);
            Assert.IsNull(attr.Description);
            Assert.IsNull(attr.GroupName);

            Assert.IsNull(attr.GetShortName());
            Assert.IsNull(attr.GetName());
            Assert.IsNull(attr.GetPrompt());
            Assert.IsNull(attr.GetDescription());
            Assert.IsNull(attr.GetGroupName());

            attr.ShortName = "theShortName";
            attr.Name = "theName";
            attr.Prompt = "thePrompt";
            attr.Description = "theDescription";
            attr.GroupName = "theGroupName";

            Assert.AreEqual("theShortName", attr.GetShortName());
            Assert.AreEqual("theName", attr.GetName());
            Assert.AreEqual("thePrompt", attr.GetPrompt());
            Assert.AreEqual("theDescription", attr.GetDescription());
            Assert.AreEqual("theGroupName", attr.GetGroupName());

            attr.ShortName = String.Empty;
            attr.Name = String.Empty;
            attr.Prompt = String.Empty;
            attr.Description = String.Empty;
            attr.GroupName = String.Empty;

            Assert.AreEqual(String.Empty, attr.GetShortName());
            Assert.AreEqual(String.Empty, attr.GetName());
            Assert.AreEqual(String.Empty, attr.GetPrompt());
            Assert.AreEqual(String.Empty, attr.GetDescription());
            Assert.AreEqual(String.Empty, attr.GetGroupName());
        }
Пример #15
0
        public void Description_Get_Set(string value)
        {
            DisplayAttribute attribute = new DisplayAttribute();

            attribute.Description = value;

            Assert.Equal(value, attribute.Description);
            Assert.Equal(value == null, attribute.GetDescription() == null);

            // Set again, to cover the setter avoiding operations if the value is the same
            attribute.Description = value;
            Assert.Equal(value, attribute.Description);
        }
        public void StringProperties_GetUnSetProperties_ReturnsNull()
        {
            var display = new DisplayAttribute();

            Assert.IsNull(display.Name);
            Assert.IsNull(display.ShortName);
            Assert.IsNull(display.Prompt);
            Assert.IsNull(display.Description);
            Assert.IsNull(display.GroupName);

            Assert.IsNull(display.GetName());
            Assert.IsNull(display.GetShortName());
            Assert.IsNull(display.GetPrompt());
            Assert.IsNull(display.GetDescription());
            Assert.IsNull(display.GetGroupName());
        }
Пример #17
0
        public static string GetDescription <TEnum>(this TEnum value)
            where TEnum : struct, IConvertible
        {
            DescriptionAttribute descriptionAttribute = value.GetAttribute <TEnum, DescriptionAttribute>();

            if (descriptionAttribute != null)
            {
                return(descriptionAttribute.Description);
            }

            DisplayAttribute displayAttribute = value.GetAttribute <TEnum, DisplayAttribute>();

            return(displayAttribute == null
                ? value.ToString(CultureInfo.InvariantCulture)
                : displayAttribute.GetDescription());
        }
Пример #18
0
        public static string GetDisplayDescriptionForEnum(object o)
        {
            string           result  = null;
            DisplayAttribute display = o.GetType()
                                       .GetMember(o.ToString())
                                       .First()
                                       .GetCustomAttributes(false)
                                       .OfType <DisplayAttribute>()
                                       .LastOrDefault();

            if (display != null)
            {
                result = display.GetDescription() ?? display.GetName();
            }

            return(result ?? o.ToString());
        }
        public void StringProperties_ReturnLiteralValues_Success()
        {
            var display = new DisplayAttribute()
            {
                Name        = "Name",
                ShortName   = "ShortName",
                Prompt      = "Prompt",
                Description = "Description",
                GroupName   = "GroupName"
            };

            Assert.AreEqual("Name", display.GetName());
            Assert.AreEqual("ShortName", display.GetShortName());
            Assert.AreEqual("Prompt", display.GetPrompt());
            Assert.AreEqual("Description", display.GetDescription());
            Assert.AreEqual("GroupName", display.GetGroupName());
        }
Пример #20
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="obj"> </param>
        /// <param name="propertyName"> </param>
        /// <returns> </returns>
        public static string GetPropertyDescription <T>(this T obj, string propertyName) where T : NotifyPropertyBase
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return(string.Empty);
            }
            Type         tp    = obj.GetType();
            PropertyInfo pi    = tp.GetProperty(propertyName);
            var          value = pi.GetValue(obj, null);

            object[] Attributes     = pi.GetCustomAttributes(false);
            string   strDescription = "";

            if (Attributes != null && Attributes.Length > 0)
            {
                var attribute = Attributes.FirstOrDefault(o => o is DisplayAttribute);

                if (attribute != null)
                {
                    try
                    {
                        DisplayAttribute vAttribute = attribute as DisplayAttribute;

                        if (vAttribute.ResourceType == typeof(Common.LanguageResource))
                        {
                            strDescription = Common.LangHelper.GetValue(vAttribute.ShortName, vAttribute.Description, "description");
                        }

                        if (String.IsNullOrWhiteSpace(strDescription))
                        {
                            if (vAttribute.ResourceType == typeof(Common.LanguageResource))
                            {
                                vAttribute.ResourceType = null;
                            }
                            strDescription = vAttribute.GetDescription();
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                }
            }
            return(strDescription);
        }
        /// <summary>
        /// Parse metadata given a binding path and entity object.
        /// </summary>
        /// <param name="bindingPath">The bindingPath is the name of the property on the entity from which to pull metadata from.  This supports dot notation.</param>
        /// <param name="entity">The entity object from which to pull metadata from.</param>
        /// <returns>The validation metadata associated with the entity and binding path.  This will return null if none exists.</returns>
        internal static ValidationMetadata ParseMetadata(string bindingPath, object entity)
        {
            if (entity != null && !String.IsNullOrEmpty(bindingPath))
            {
                Type         entityType = entity.GetType();
                PropertyInfo prop       = GetProperty(entityType, bindingPath);
                if (prop != null)
                {
                    ValidationMetadata newVMD     = new ValidationMetadata();
                    object[]           attributes = prop.GetCustomAttributes(false);
                    foreach (object propertyAttribute in attributes)
                    {
                        // Loop through each attribute and update the VMD as appropriate

                        // RequiredField
                        RequiredAttribute reqAttribute = propertyAttribute as RequiredAttribute;
                        if (reqAttribute != null)
                        {
                            newVMD.IsRequired = true;
                            continue;
                        }

                        // Display attribute parsing
                        DisplayAttribute displayAttribute = propertyAttribute as DisplayAttribute;
                        if (displayAttribute != null)
                        {
                            newVMD.Description = displayAttribute.GetDescription();
                            newVMD.Caption     = displayAttribute.GetName();
                            continue;
                        }
                    }
                    if (newVMD.Caption == null)
                    {
                        // If the name is not defined via the DisplayAttribute, use the property name.
                        newVMD.Caption = prop.Name;

                        // Caption can be set to empty string to have an empty Caption and not default
                        // to the property name.
                    }

                    return(newVMD);
                }
            }
            return(null);
        }
        public void StringProperties_ReturnLocalizedValues_Success()
        {
            var display = new DisplayAttribute()
            {
                ResourceType = typeof(GoodResources),
                Name         = "Name",
                ShortName    = "ShortName",
                Prompt       = "Prompt",
                Description  = "Description",
                GroupName    = "GroupName"
            };

            Assert.AreEqual(GoodResources.Name, display.GetName());
            Assert.AreEqual(GoodResources.ShortName, display.GetShortName());
            Assert.AreEqual(GoodResources.Prompt, display.GetPrompt());
            Assert.AreEqual(GoodResources.Description, display.GetDescription());
            Assert.AreEqual(GoodResources.GroupName, display.GetGroupName());
        }
Пример #23
0
        public void Attributes_Cities_Entity_Display_On_Property_Resourced()
        {
            DisplayAttribute attr = ExpectPropertyAttribute <DisplayAttribute>(typeof(City), "Name");

            Assert.AreEqual("CityCaption", attr.ShortName);
            Assert.AreEqual("Name of City", attr.GetShortName());

            Assert.AreEqual("CityName", attr.Name);
            Assert.AreEqual("CityName", attr.GetName());

            Assert.AreEqual("CityPrompt", attr.Prompt);
            Assert.AreEqual("Enter the city name", attr.GetPrompt());

            Assert.AreEqual("CityHelpText", attr.Description);
            Assert.AreEqual("This is the name of the city", attr.GetDescription());

            Assert.AreEqual(typeof(Cities.Cities_Resources), attr.ResourceType);;
        }
Пример #24
0
        public ModelMetadata BuildUp(ModelMetadata metadata, IEnumerable <Attribute> attributes,
                                     Type containerType,
                                     Func <object> modelAccessor,
                                     Type modelType,
                                     string propertyName)
        {
            // Prefer [Display(Name="")] to [DisplayName]
            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();

            if (display != null)
            {
                string name = display.GetName();
                if (name != null)
                {
                    metadata.DisplayName = name;
                }

                // There was no 3.5 way to set these values
                metadata.Description      = display.GetDescription();
                metadata.ShortDisplayName = display.GetShortName();
                metadata.Watermark        = display.GetPrompt();
            }

            // Prefer [Editable] to [ReadOnly]
            EditableAttribute editable = attributes.OfType <EditableAttribute>().FirstOrDefault();

            if (editable != null)
            {
                metadata.IsReadOnly = !editable.AllowEdit;
            }

            // If [DisplayFormat(HtmlEncode=false)], set a data type name of "Html"
            // (if they didn't already set a data type)
            DisplayFormatAttribute displayFormat = attributes.OfType <DisplayFormatAttribute>().FirstOrDefault();

            if (displayFormat != null &&
                !displayFormat.HtmlEncode &&
                String.IsNullOrWhiteSpace(metadata.DataTypeName))
            {
                metadata.DataTypeName = DataType.Html.ToString();
            }

            return(metadata);
        }
Пример #25
0
        public void Ctor()
        {
            DisplayAttribute attribute = new DisplayAttribute();
            Assert.Null(attribute.ShortName);
            Assert.Null(attribute.GetShortName());

            Assert.Null(attribute.Name);
            Assert.Null(attribute.GetName());

            Assert.Null(attribute.Description);
            Assert.Null(attribute.GetDescription());

            Assert.Null(attribute.Prompt);
            Assert.Null(attribute.GetPrompt());

            Assert.Null(attribute.GroupName);
            Assert.Null(attribute.GetGroupName());

            Assert.Null(attribute.ResourceType);
        }
        public void AllProperties_WriteOnlyResource_ThrowsInvalidOperationException()
        {
            var resourceType = typeof(BadResources);
            var resourceKey  = "WriteOnlyString";
            var display      = new DisplayAttribute()
            {
                ResourceType = resourceType,
                Name         = resourceKey,
                ShortName    = resourceKey,
                Prompt       = resourceKey,
                Description  = resourceKey,
                GroupName    = resourceKey
            };

            ExceptionAssert.Throws <InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
            ExceptionAssert.Throws <InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
            ExceptionAssert.Throws <InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
            ExceptionAssert.Throws <InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
            ExceptionAssert.Throws <InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
        }
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType,
                                                        Func <object> modelAccessor, Type modelType, string propertyName)
        {
            ModelMetadata metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();

            if (display != null)
            {
                string name = display.GetName();
                if (name != null)
                {
                    metadata.DisplayName = name;
                }

                metadata.Description      = display.GetDescription();
                metadata.ShortDisplayName = display.GetShortName();
                metadata.Watermark        = display.GetPrompt();
            }

            EditableAttribute editable = attributes.OfType <EditableAttribute>().FirstOrDefault();

            if (editable != null)
            {
                metadata.IsReadOnly = !editable.AllowEdit;
            }

            DisplayFormatAttribute displayFormat = attributes.OfType <DisplayFormatAttribute>().FirstOrDefault();

            if (displayFormat != null && !displayFormat.HtmlEncode && String.IsNullOrWhiteSpace(metadata.DataTypeName))
            {
                metadata.DataTypeName = DataType.Html.ToString();
            }

            foreach (IMetadataAware awareAttribute in attributes.OfType <IMetadataAware>())
            {
                awareAttribute.OnMetadataCreated(metadata);
            }

            return(metadata);
        }
Пример #28
0
        /// <summary>
        ///     创建实体属性元数据。
        /// </summary>
        /// <param name="property"></param>
        /// <param name="attributes"></param>
        /// <returns></returns>
        protected virtual IEntityPropertyMetadata CreateEntityPropertyMetadata(PropertyInfo property,
                                                                               IEnumerable <Attribute> attributes)
        {
            var result = new EntityPropertyMetadata(property)
            {
                IsSearcher = attributes.Any(x => x is SearcherAttribute),
                IsRequired = attributes.Any(x => x is RequiredAttribute),
                IsKey      = attributes.Any(x => x is KeyAttribute),
                DataType   = EntityExtensions.GetDataType(property)
            };

            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();
            string           name    = null;

            if (display != null)
            {
                result.Description = display.GetDescription();
                result.Order       = display.GetOrder() ?? ModelMetadata.DefaultOrder;
                result.GroupName   = display.GetGroupName();
                name = display.GetName();
            }

            if (name != null)
            {
                result.Name = name;
            }
            else
            {
                DisplayNameAttribute displayNameAttribute = attributes.OfType <DisplayNameAttribute>().FirstOrDefault();
                if (displayNameAttribute != null)
                {
                    result.Name = displayNameAttribute.DisplayName;
                }
            }
            if (string.IsNullOrEmpty(result.Name))
            {
                result.Name = property.Name;
            }

            return(result);
        }
Пример #29
0
        public void Ctor()
        {
            DisplayAttribute attribute = new DisplayAttribute();

            Assert.Null(attribute.ShortName);
            Assert.Null(attribute.GetShortName());

            Assert.Null(attribute.Name);
            Assert.Null(attribute.GetName());

            Assert.Null(attribute.Description);
            Assert.Null(attribute.GetDescription());

            Assert.Null(attribute.Prompt);
            Assert.Null(attribute.GetPrompt());

            Assert.Null(attribute.GroupName);
            Assert.Null(attribute.GetGroupName());

            Assert.Null(attribute.ResourceType);
        }
Пример #30
0
        /// <summary>
        /// Returns the value of the DescriptionAttribute for an enum value,
        /// or null if there isn't one.
        /// </summary>
        private static string EnumDescriptionValue(Enum value)
        {
            var enumType = value.GetType();

            // https://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value/1799401#1799401
            var member = enumType.GetMember(value.ToString()).First();
            DescriptionAttribute description = (DescriptionAttribute)member.GetCustomAttributes(typeof(DescriptionAttribute)).FirstOrDefault();

            if (description != null)
            {
                return(description.Description);
            }

            DisplayAttribute displayAttribute = (DisplayAttribute)member.GetCustomAttributes(typeof(DisplayAttribute)).FirstOrDefault();

            if (displayAttribute != null)
            {
                return(displayAttribute.GetDescription() ?? displayAttribute.GetName() ?? displayAttribute.GetShortName());
            }

            return(null);
        }
Пример #31
0
        public static string GetDisplayName(object value)
        {
            if (value == null)
            {
                return(null);
            }

            DisplayAttribute displayAttribute = DisplayAttributeHelper.GetDisplayAttribute(value.GetType(), value.ToString());

            if (displayAttribute == null)
            {
                return(value.ToString());
            }

            string displayName = displayAttribute.GetName();

            if (displayName.IsNullOrEmpty())
            {
                displayName = displayAttribute.GetDescription();
            }

            return(displayName);
        }
Пример #32
0
        internal static Metadatos ParseMetadata(string bindingPath, object entity)
        {
            if (entity != null && !string.IsNullOrEmpty(bindingPath))
            {
                PropertyInfo property = GetProperty(entity.GetType(), bindingPath);
                if (property != null)
                {
                    Metadatos validationMetadata = new Metadatos();
                    foreach (object obj in property.GetCustomAttributes(false))
                    {
                        if (obj is RequiredAttribute)
                        {
                            validationMetadata.EsRequerido = true;
                        }
                        else
                        {
                            DisplayAttribute displayAttribute = obj as DisplayAttribute;
                            if (displayAttribute != null)
                            {
                                validationMetadata.Descripcion = displayAttribute.GetDescription();
                                validationMetadata.Nombre      = displayAttribute.GetName();
                            }
                        }
                    }

                    if (validationMetadata.Nombre == null)
                    {
                        validationMetadata.Nombre = property.Name;
                    }

                    return(validationMetadata);
                }
            }

            return(null);
        }
        public void DisplayAttribute_Fail_No_Getter_Resource() {
            DisplayAttribute attr = new DisplayAttribute();
            attr.ResourceType = typeof(DisplayAttribute_Resources);
            attr.ShortName = "NoGetter";
            attr.Name = "NoGetter";
            attr.Prompt = "NoGetter";
            attr.Description = "NoGetter";
            attr.GroupName = "NoGetter";

            string shortNameError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "ShortName", attr.ResourceType.FullName, attr.ShortName);
            string nameError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "Name", attr.ResourceType.FullName, attr.Name);
            string promptError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "Prompt", attr.ResourceType.FullName, attr.Prompt);
            string descriptionError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "Description", attr.ResourceType.FullName, attr.Description);
            string groupNameError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "GroupName", attr.ResourceType.FullName, attr.GroupName);

            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetShortName(); }, shortNameError);
            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetName(); }, nameError);
            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetPrompt(); }, promptError);
            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetDescription(); }, descriptionError);
            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetGroupName(); }, groupNameError);
        }
 public void DisplayAttribute_GetDescription_Does_Not_Fall_Back() {
     DisplayAttribute attr = new DisplayAttribute { ShortName = "ShortName", Name = "Name", Prompt = "Prompt", GroupName = "GroupName" };
     Assert.IsNull(attr.GetDescription(), "Description should NOT fall back onto any other values");
 }
        public void DisplayAttribute_Resourced_Properties_Wrong_Keys() {
            DisplayAttribute attr = new DisplayAttribute();

            attr.ResourceType = typeof(DisplayAttribute_Resources);

            attr.ShortName = "notAKey1";
            attr.Name = "notAKey2";
            attr.Prompt = "notAKey3";
            attr.Description = "notAKey4";
            attr.GroupName = "notAKey5";

            Assert.AreEqual("notAKey1", attr.ShortName);
            Assert.AreEqual("notAKey2", attr.Name);
            Assert.AreEqual("notAKey3", attr.Prompt);
            Assert.AreEqual("notAKey4", attr.Description);
            Assert.AreEqual("notAKey5", attr.GroupName);

            string shortNameError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "ShortName", attr.ResourceType.FullName, attr.ShortName);
            string nameError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "Name", attr.ResourceType.FullName, attr.Name);
            string promptError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "Prompt", attr.ResourceType.FullName, attr.Prompt);
            string descriptionError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "Description", attr.ResourceType.FullName, attr.Description);
            string groupNameError = String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.LocalizableString_LocalizationFailed, "GroupName", attr.ResourceType.FullName, attr.GroupName);

            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetShortName(); }, shortNameError);
            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetName(); }, nameError);
            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetPrompt(); }, promptError);
            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetDescription(); }, descriptionError);
            ExceptionHelper.ExpectException<InvalidOperationException>(() => { string s = attr.GetGroupName(); }, groupNameError);
        }
Пример #36
0
        public void Description_Get_Set(string value)
        {
            DisplayAttribute attribute = new DisplayAttribute();
            attribute.Description = value;

            Assert.Equal(value, attribute.Description);
            Assert.Equal(value == null, attribute.GetDescription() == null);

            // Set again, to cover the setter avoiding operations if the value is the same
            attribute.Description = value;
            Assert.Equal(value, attribute.Description);
        }
 public string GetDescription()
 {
     return(_innerAttribute.GetDescription());
 }