Esempio n. 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);
        }
Esempio n. 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);
        }
 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);
        }
Esempio n. 6
0
        private IHtmlString GetTitle(Expression <Func <T, TValue> > expression)
        {
            MemberExpression body    = expression.Body as MemberExpression;
            DisplayAttribute display = body?.Member.GetCustomAttribute <DisplayAttribute>();

            return(new HtmlString(display?.GetShortName()));
        }
Esempio n. 7
0
        private String TitleFor(Expression <Func <T, TValue> > expression)
        {
            MemberExpression?body    = expression.Body as MemberExpression;
            DisplayAttribute?display = body?.Member.GetCustomAttribute <DisplayAttribute>();

            return(display?.GetShortName() ?? "");
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="obj"> </param>
        /// <param name="propertyName"> </param>
        /// <returns> </returns>
        public static string GetPropertyShortName <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   strShortName = "";

            if (Attributes != null && Attributes.Length > 0)
            {
                foreach (object attribute in Attributes)
                {
                    if (attribute is DisplayAttribute)
                    {
                        try
                        {
                            DisplayAttribute vAttribute = attribute as DisplayAttribute;
                            strShortName = vAttribute.GetShortName();
                            break;
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                        }
                    }
                }
            }
            return(strShortName);
        }
Esempio n. 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);
        }
Esempio n. 10
0
        public void DisplayAttribute_GetShortName_Falls_Back_Onto_Localized_Name()
        {
            DisplayAttribute attr = new DisplayAttribute {
                ResourceType = typeof(DisplayAttribute_Resources), Name = "Resource1"
            };

            Assert.AreEqual(DisplayAttribute_Resources.Resource1, attr.GetShortName(), "GetShortName should return the localized Name when ShortName is null");
        }
Esempio n. 11
0
        public void DisplayAttribute_GetShortName_Falls_Back_Onto_NonLocalized_Name()
        {
            DisplayAttribute attr = new DisplayAttribute {
                Name = "Name"
            };

            Assert.AreEqual("Name", attr.GetShortName(), "GetShortName should return the Name when ShortName is null");
        }
        public void ShortName_ReturnsName_WhenNotSet()
        {
            var display = new DisplayAttribute()
            {
                Name = "Name"
            };

            Assert.AreEqual("Name", display.GetShortName());
        }
Esempio n. 13
0
        /// <summary>
        /// Applies the display attributes.
        /// </summary>
        /// <param name="attr">The attribute.</param>
        /// <param name="pi">The property info.</param>
        private static void ApplyDisplayAttributes(this AttributeModel attr, PropertyInfo pi)
        {
            DisplayAttribute disp = (DisplayAttribute)pi.GetCustomAttribute(typeof(DisplayAttribute));

            attr.DisplayName      = disp == null ? pi.Name : disp.GetName();
            attr.DisplayGroupName = disp?.GetGroupName();
            attr.ShortName        = disp?.GetShortName();
            attr.Prompt           = disp?.GetPrompt();
        }
Esempio n. 14
0
        public string GetShortName()
        {
            var localizedString = GetLocalizedString(_displayAttribute.ShortName);

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

            return(localizedString);
        }
Esempio n. 15
0
        public void GridColumn_DisplayAttribute_SetsTitleFromDisplayShortName()
        {
            DisplayAttribute display = typeof(GridModel).GetProperty("ShortText").GetCustomAttribute <DisplayAttribute>();

            column = new GridColumn <GridModel, Object>(grid, model => model.ShortText);

            String expected = display.GetShortName();
            String actual   = column.Title.ToString();

            Assert.Equal(expected, actual);
        }
Esempio n. 16
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());
        }
Esempio n. 17
0
        public void ShortName_Get_Set(string value)
        {
            DisplayAttribute attribute = new DisplayAttribute();
            attribute.ShortName = value;

            Assert.Equal(value, attribute.ShortName);
            Assert.Equal(value == null, attribute.GetShortName() == null);

            // Set again, to cover the setter avoiding operations if the value is the same
            attribute.ShortName = value;
            Assert.Equal(value, attribute.ShortName);
        }
Esempio n. 18
0
        /// <summary>
        /// Finds the PropertyInfo for the specified property path within this Type, and returns
        /// the value of GetShortName on its DisplayAttribute, if one exists. GetShortName will return
        /// the value of Name if there is no ShortName specified.
        /// </summary>
        /// <param name="type">Type to search</param>
        /// <param name="propertyPath">property path</param>
        /// <returns>DisplayAttribute.ShortName if it exists, null otherwise</returns>
        internal static string GetDisplayName(this Type type, string propertyPath)
        {
            PropertyInfo propertyInfo = type.GetNestedProperty(propertyPath);

            if (propertyInfo != null)
            {
                DisplayAttribute displayAttribute = propertyInfo.GetCustomAttributes().OfType <DisplayAttribute>().FirstOrDefault();
                return(displayAttribute == null ? null : displayAttribute.GetShortName());
            }

            return(null);
        }
        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());
        }
Esempio n. 20
0
        public void ShortName_Get_Set(string value)
        {
            DisplayAttribute attribute = new DisplayAttribute();

            attribute.ShortName = value;

            Assert.Equal(value, attribute.ShortName);
            Assert.Equal(value == null, attribute.GetShortName() == null);

            // Set again, to cover the setter avoiding operations if the value is the same
            attribute.ShortName = value;
            Assert.Equal(value, attribute.ShortName);
        }
        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());
        }
        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());
        }
Esempio n. 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);;
        }
        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());
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="obj"> </param>
        /// <param name="propertyName"> </param>
        /// <returns> </returns>
        public static string GetPropertyShortName <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   strShortName = "";

            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))
                        {
                            strShortName = Common.LangHelper.GetValue(vAttribute.ShortName, vAttribute.ShortName, "shortname");
                        }

                        if (String.IsNullOrWhiteSpace(strShortName))
                        {
                            if (vAttribute.ResourceType == typeof(Common.LanguageResource))
                            {
                                vAttribute.ResourceType = null;
                            }
                            strShortName = vAttribute.GetShortName();
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                }
            }
            return(strShortName);
        }
        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);
        }
Esempio n. 27
0
        /// <summary>
        /// Finds the PropertyInfo for the specified property path within this Type, and returns
        /// the value of GetShortName on its DisplayAttribute, if one exists. GetShortName will return
        /// the value of Name if there is no ShortName specified.
        /// </summary>
        /// <param name="type">Type to search</param>
        /// <param name="propertyPath">property path</param>
        /// <returns>DisplayAttribute.ShortName if it exists, null otherwise</returns>
        internal static string GetDisplayName(this Type type, string propertyPath)
        {
            PropertyInfo propertyInfo = type.GetNestedProperty(propertyPath);

            if (propertyInfo != null)
            {
                object[] attributes = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true);
                if (attributes != null && attributes.Length > 0)
                {
                    Debug.Assert(attributes.Length == 1);
                    DisplayAttribute displayAttribute = attributes[0] as DisplayAttribute;
                    if (displayAttribute != null)
                    {
                        return(displayAttribute.GetShortName());
                    }
                }
            }
            return(null);
        }
        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));
        }
Esempio n. 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);
        }
 internal static string GetDisplayName(this Type objectType, string propertyPath)
 {
     if (objectType != null)
     {
         System.Reflection.PropertyInfo property = objectType.GetProperty(propertyPath);
         if (property != null)
         {
             object[] customAttributes = property.GetCustomAttributes(typeof(DisplayAttribute), true);
             if ((customAttributes != null) && (customAttributes.Length > 0))
             {
                 DisplayAttribute attribute = customAttributes[0] as DisplayAttribute;
                 if (attribute != null)
                 {
                     return(attribute.GetShortName());
                 }
             }
         }
     }
     return(propertyPath);
 }
        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);
        }
Esempio n. 32
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);
        }
Esempio n. 33
0
        public static Dictionary <int, displayEnum> ToArray(Type enumType)
        {
            Dictionary <int, displayEnum> listEnumField = new Dictionary <int, displayEnum>();
            Type type = enumType;

            foreach (var evalue in type.GetEnumValues())
            {
                var val       = (int)evalue;
                var valueName = type.GetField(evalue.ToString());

                DisplayAttribute displayAtt = valueName.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute;
                var displayEnumValue        = new displayEnum();
                if (displayAtt != null)
                {
                    displayEnumValue.displayLabel = displayAtt.GetName();
                    displayEnumValue.displayCode  = displayAtt.GetShortName();
                }

                listEnumField.Add((int)evalue, displayEnumValue);
            }
            return(listEnumField);
        }
 public void DisplayAttribute_GetShortName_Falls_Back_Onto_NonLocalized_Name() {
     DisplayAttribute attr = new DisplayAttribute { Name = "Name" };
     Assert.AreEqual("Name", attr.GetShortName(), "GetShortName should return the Name when ShortName is null");
 }
 public void DisplayAttribute_GetShortName_Falls_Back_Onto_Localized_Name() {
     DisplayAttribute attr = new DisplayAttribute { ResourceType = typeof(DisplayAttribute_Resources), Name = "Resource1" };
     Assert.AreEqual(DisplayAttribute_Resources.Resource1, attr.GetShortName(), "GetShortName should return the localized Name when ShortName is null");
 }
 public string GetShortName()
 {
     return(_innerAttribute.GetShortName());
 }
        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_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);
        }