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_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); }
public void DisplayAttribute_GetGroupName_Does_Not_Fall_Back() { DisplayAttribute attr = new DisplayAttribute { ShortName = "ShortName", Name = "Name", Description = "Description", Prompt = "Prompt" }; Assert.IsNull(attr.GetGroupName(), "GetGroupName should NOT fall back onto any other values"); }
/// <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(); }
public static string DisplayGroupNameFor <TModel, TDataModel>(this IHtmlHelper <TModel> helper, Expression <Func <TModel, TDataModel> > expression) where TModel : class where TDataModel : class { Type modelType = typeof(TDataModel); DisplayAttribute displayAttribute = GetDisplayAttributeFor(modelType); string gruoupName = displayAttribute?.GetGroupName() ?? modelType.Name; return(gruoupName); }
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 GroupName_Get_Set(string value) { DisplayAttribute attribute = new DisplayAttribute(); attribute.GroupName = value; Assert.Equal(value, attribute.GroupName); Assert.Equal(value == null, attribute.GetGroupName() == null); // Set again, to cover the setter avoiding operations if the value is the same attribute.GroupName = value; Assert.Equal(value, attribute.GroupName); }
private void EnsureDisplay() { DisplayAttribute customAttribute = GetCustomAttribute <DisplayAttribute>(); if (customAttribute == null) { return; } meta.ShouldAdd = customAttribute.GetAutoGenerateField(); meta.GroupDisplayName = customAttribute.GetGroupName(); meta.HeaderText = customAttribute.GetName(); meta.Watermark = customAttribute.GetPrompt(); }
private static int GetColumnOrder(MetaColumn column, IDictionary <string, int> groupings) { DisplayAttribute displayAttribute = column.Metadata.DisplayAttribute; if (displayAttribute != null) { int num; string groupName = displayAttribute.GetGroupName(); if (!string.IsNullOrEmpty(groupName) && groupings.TryGetValue(groupName, out num)) { return(num); } } return(GetColumnOrder(column)); }
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()); }
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()); }
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)); }
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); }
/// <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); }
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_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); }
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); }
public string GetGroupName() { return(_innerAttribute.GetGroupName()); }
public string GetGroupName() => _display.GetGroupName();
public static Metadata Create(MemberInfo memberInfo) { if (memberInfo == null) { throw new ArgumentNullException("memberInfo"); } Metadata metadata = new Metadata(); // Name metadata.Name = memberInfo.Name; // DisplayAttribute DisplayAttribute displayAttribute = memberInfo.GetCustomAttribute <DisplayAttribute>(); if (displayAttribute != null) { metadata.DisplayName = displayAttribute.GetName(); metadata.ShortName = displayAttribute.GetShortName(); metadata.GroupName = displayAttribute.GetGroupName(); metadata.Description = displayAttribute.GetDescription(); int?order = displayAttribute.GetOrder(); if (order != null) { metadata.Order = order.Value; } } if (metadata.DisplayName == null) { metadata.DisplayName = ConvertUtilities.Decamelize(metadata.Name); } // DataType DataTypeAttribute dataTypeAttribute = memberInfo.GetCustomAttribute <DataTypeAttribute>(); if (dataTypeAttribute != null) { metadata.DataType = dataTypeAttribute.GetDataTypeName(); Fill(metadata, dataTypeAttribute.DisplayFormat); } if (metadata.DataType == null) { PropertyInfo pi = memberInfo as PropertyInfo; if (pi != null) { metadata.DataType = pi.PropertyType.AssemblyQualifiedName; } else { FieldInfo fi = memberInfo as FieldInfo; if (fi != null) { metadata.DataType = fi.FieldType.AssemblyQualifiedName; } } } // DisplayFormat DisplayFormatAttribute displayFormatAttribute = memberInfo.GetCustomAttribute <DisplayFormatAttribute>(); if (displayFormatAttribute != null) { Fill(metadata, displayFormatAttribute); } // ScaffoldColumnAttribute ScaffoldColumnAttribute scaffoldColumnAttribute = memberInfo.GetCustomAttribute <ScaffoldColumnAttribute>(); if (scaffoldColumnAttribute != null) { metadata.Hidden = scaffoldColumnAttribute.Scaffold; } return(metadata); }