EOS/Models/Util/DisplayNameHelper.cs
Nidhi Bhargava f0c1ab20e1 code push
2025-09-04 16:25:07 +05:30

108 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Web;
namespace Helper
{
/// <summary>
/// This classs contain property releted method
/// </summary>
public static class DisplayNameHelper
{
/// <summary>
/// This mehtod is used for get display name of class object property
/// </summary>
/// <param name="obj">Represent object of object class contain proerties</param>
/// <param name="propertyName">Represent name of the property</param>
/// <returns></returns>
public static string GetDisplayName(object obj, string propertyName)
{
if (obj == null) return null;
return GetDisplayName(obj.GetType(), propertyName);
}
/// <summary>
/// This method is used for returns value of display name.
/// </summary>
/// <param name="type">Type of the property</param>
/// <param name="propertyName">Represent name of the property</param>
/// <returns>string</returns>
public static string GetDisplayName(Type type, string propertyName)
{
var property = type.GetProperty(propertyName);
if (property == null) return null;
return GetDisplayName(property);
}
/// <summary>
/// This action gives display name of given property of model
/// </summary>
/// <param name="property">PropertyInfo: array of properties informaion</param>
/// <returns>string</returns>
public static string GetDisplayName(PropertyInfo property)
{
// return attribute Display Name of given property.
var attrName = GetAttributeDisplayName(property);
if (!string.IsNullOrEmpty(attrName))
return attrName;
var metaName = GetMetaDisplayName(property);
if (!string.IsNullOrEmpty(metaName))
return metaName;
return property.Name.ToString();
}
/// <summary>
/// Returns property "DisplayAttribute" value.
/// </summary>
/// <param name="property"></param>
/// <returns>string</returns>
private static string GetAttributeDisplayName(PropertyInfo property)
{
foreach (var item in property.CustomAttributes)
{
if (item.AttributeType.Name.Equals("DisplayAttribute"))
{
return item.NamedArguments[0].TypedValue.Value.ToString();
}
}
return null;
}
//private static string GetAttributeDisplayName(PropertyInfo property)
//{
// var atts = property.GetCustomAttributes(
// typeof(DisplayNameAttribute), true);
// if (atts.Length == 0)
// return null;
// return (atts[0] as DisplayNameAttribute).DisplayName;
//}
/// <summary>
/// Returns mata data of attribute DisplayName.
/// </summary>
/// <param name="property">PropertyInfo of property</param>
/// <returns>string</returns>
private static string GetMetaDisplayName(PropertyInfo property)
{
var atts = property.DeclaringType.GetCustomAttributes(
typeof(MetadataTypeAttribute), true);
if (atts.Length == 0)
return null;
var metaAttr = atts[0] as MetadataTypeAttribute;
var metaProperty =
metaAttr.MetadataClassType.GetProperty(property.Name);
if (metaProperty == null)
return null;
return GetAttributeDisplayName(metaProperty);
}
}
}