本文已参与「新人创作礼」活动,一起开启掘金创作之路
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Wp.Utils.Utils
{
/// <summary>
/// 属性帮助类
/// </summary>
public class PropertyUtil
{
/// <summary>
/// 获取属性描述
/// </summary>
/// <typeparam name="T">类类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <returns></returns>
public static string GetPropertyDescription<T>(string propertyName) where T : class
{
object[] objs = typeof(T).GetProperty(propertyName).GetCustomAttributes(typeof(DescriptionAttribute), true);
return objs.Length > 0 ? ((DescriptionAttribute)objs[0]).Description : string.Empty;
}
/// <summary>
/// 获取类属性描述和属性名
/// 属性描述为key
/// </summary>
/// <typeparam name="T">类类型</typeparam>
/// <param name="obj">类实例</param>
/// <param name="exceptType">不需返回的属性类型</param>
/// <param name="bindingFlags">属性类型</param>
/// <returns></returns>
public static Dictionary<string, string> GetPropertyNameKeyIsDescription<T>(
T obj,
List<Type> exceptType = null,
BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) where T : class
{
try
{
var res = new Dictionary<string, string>();
if (exceptType != null)
{
foreach (PropertyInfo p in typeof(T).GetProperties(bindingFlags))
{
if (exceptType.Contains(p.PropertyType))
{
//
}
else
{
var v = p.GetValue(obj);
res.Add(((DescriptionAttribute)p.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()).Description, p.Name);
}
}
}
else
{
foreach (PropertyInfo p in typeof(T).GetProperties(bindingFlags))
{
var v = p.GetValue(obj);
res.Add(((DescriptionAttribute)p.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()).Description, p.Name);
}
}
return res;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 获取类属性名和属性值
/// 属性名为key
/// </summary>
/// <typeparam name="T">类类型</typeparam>
/// <param name="obj">类实例</param>
/// <param name="exceptType">不需返回的属性类型</param>
/// <param name="bindingFlags">属性类型</param>
/// <returns></returns>
public static Dictionary<string, object> GetPropertyValueKeyIsName<T>(
T obj,
List<Type> exceptType = null,
BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) where T : class
{
try
{
var res = new Dictionary<string, object>();
if (exceptType != null)
{
foreach (PropertyInfo p in typeof(T).GetProperties(bindingFlags))
{
if (exceptType.Contains(p.PropertyType))
{
//
}
else
{
var v = p.GetValue(obj);
res.Add(p.Name, v);
}
}
}
else
{
foreach (PropertyInfo p in typeof(T).GetProperties(bindingFlags))
{
var v = p.GetValue(obj);
res.Add(p.Name, v);
}
}
return res;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 获取类属性描述和属性值
/// 属性描述为Key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="exceptType"></param>
/// <param name="bindingFlags"></param>
/// <returns></returns>
public static Dictionary<string, object> GetPropertyValueKeyIsDescription<T>(
T obj,
List<Type> exceptType = null,
BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) where T : class
{
try
{
var res = new Dictionary<string, object>();
if (exceptType != null)
{
foreach (PropertyInfo p in typeof(T).GetProperties(bindingFlags))
{
if (exceptType.Contains(p.PropertyType))
{
//
}
else
{
var v = p.GetValue(obj);
res.Add(((DescriptionAttribute)p.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()).Description, v);
}
}
}
else
{
foreach (PropertyInfo p in typeof(T).GetProperties(bindingFlags))
{
var v = p.GetValue(obj);
res.Add(((DescriptionAttribute)p.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()).Description, v);
}
}
return res;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 对象转字符串
/// 将对象属性值输出,格式为属性名:属性值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="exceptType"></param>
/// <param name="bindingFlags"></param>
/// <returns></returns>
public static string ObjectToStringByNameValue<T>(
T obj,
List<Type> exceptType = null,
BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) where T : class
{
var dic = GetPropertyValueKeyIsName(obj, exceptType, bindingFlags);
var res = new StringBuilder();
foreach (var item in dic)
{
res.AppendLine($"{item.Key}:{item.Value}");
}
return res.ToString();
}
/// <summary>
/// 对象转字符串
/// 将对象属性值输出,格式为属性描述:属性值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="exceptType"></param>
/// <param name="bindingFlags"></param>
/// <returns></returns>
public static string ObjectToStringByDescriptionValue<T>(T obj,
List<Type> exceptType = null,
BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) where T : class
{
var res = new StringBuilder();
var dic = GetPropertyValueKeyIsDescription(obj, exceptType, bindingFlags);
foreach (var item in dic)
{
res.AppendLine($"{item.Key}:{item.Value}");
}
return res.ToString();
}
}
}