C# .NET Core获取类属性/属性值,是否有属性/属性值,获取实体层描述,枚举描述,枚举英文获取枚举描述[Description(“xxx“)]

属性描述

定义为PropertyInfo的扩展方法

/// 
/// 获取属性描述
/// 
/// 
/// 
public static string GetDesc(this PropertyInfo t)
{
	string des = ((DescriptionAttribute)Attribute.GetCustomAttribute(t, typeof(DescriptionAttribute)))?.Description ?? null;// 属性描述
	return des;
}

属性相关操作

是否拥有某属性,获取某属性值,设置某属性值,是否拥有某字段,获取某字段值,设置某字段值,获取实体层描述

private static BindingFlags _bindingFlags { get; }= BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;
/// 
/// 是否拥有某属性
/// 
///  对象 
///  属性名 
///  
public static bool ContainsProperty(this object obj, string propertyName)
{
    return obj.GetType().GetProperty(propertyName, _bindingFlags) != null;
}

/// 
/// 获取某属性值
/// 
///  对象 
///  属性名 
///  
public static object GetPropertyValue(this object obj, string propertyName)
{
    return obj.GetType().GetProperty(propertyName, _bindingFlags).GetValue(obj);
}

/// 
/// 设置某属性值
/// 
///  对象 
///  属性名 
///  值 
///  
public static void SetPropertyValue(this object obj, string propertyName, object value)
{
    obj.GetType().GetProperty(propertyName, _bindingFlags).SetValue(obj, value);
}

/// 
/// 是否拥有某字段
/// 
///  对象 
///  字段名 
///  
public static bool ContainsField(this object obj, string fieldName)
{
    return obj.GetType().GetField(fieldName, _bindingFlags) != null;
}

/// 
/// 获取某字段值
/// 
///  对象 
///  字段名 
///  
public static object GetGetFieldValue(this object obj, string fieldName)
{
    return obj.GetType().GetField(fieldName, _bindingFlags).GetValue(obj);
}

/// 
/// 设置某字段值
/// 
///  对象 
///  字段名 
///  值 
///  
public static void SetFieldValue(this object obj, string fieldName, object value)
{
    obj.GetType().GetField(fieldName, _bindingFlags).SetValue(obj, value);
}

/// 
/// 获取某字段值
/// 
///  对象 
///  方法名 
///  
public static MethodInfo GetMethod(this object obj, string methodName)
{
    return obj.GetType().GetMethod(methodName, _bindingFlags);
}
/// 
/// 获取实体层描述
/// 
///  
///  
///  
public static string GetEntityDescription(this Type value, string Name)
{
    PropertyDescriptor s = TypeDescriptor.GetProperties(value)[Name];
    DescriptionAttribute description = s == null ? null : s.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;
    return description == null ? "" : description.Description;
}

/// 
/// 获取实体层描述
/// 
///  
///  
public static string GetEntityDescription(this PropertyDescriptor s)
{
    DescriptionAttribute description = s == null ? null : s.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;
    return description == null ? "" : description.Description;
}

枚举描述

定义为扩展方法

  /// 
  /// 获取枚举描述
  /// 
  ///  枚举值 
  ///  
  public static string GetDescription(this Enum value)
  {
      DescriptionAttribute attribute = value.GetType()
          .GetField(value.ToString())
          .GetCustomAttributes(typeof(DescriptionAttribute), false)
          .SingleOrDefault() as DescriptionAttribute;
      return attribute == null ? value.ToString() : attribute.Description;
  }

  /// 
  /// 知道枚举,传入枚举英文,获取描述
  /// 
  ///  
  ///  
  ///  
  public static string GetDescription(this Type value, string Name)
  {
      DescriptionAttribute attribute = value.GetField(Name)
          .GetCustomAttributes(typeof(DescriptionAttribute), false)
          .SingleOrDefault() as DescriptionAttribute;
      return attribute == null ? value.ToString() : attribute.Description;
  }

你可能感兴趣的:(C#,.NET,Core,c#,属性)