using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Reflection.Emit;
using System.Linq.Expressions;
namespace Util
{
/// <summary>
/// 属性值动态获取和赋值(get、set)
/// </summary>
public class PropertyUtil
{
/// <summary>
/// 反射获取对象的属性值
/// </summary>
/// <param name="obj"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static object ReflectGetter(object obj, string propertyName)
{
var type = obj.GetType();
var propertyInfo = type.GetProperty(propertyName);
var propertyValue = propertyInfo.GetValue(obj);
return propertyValue;
}
/// <summary>
/// 反射设置对象的属性值
/// </summary>
/// <param name="obj"></param>
/// <param name="propertyName"></param>
/// <param name="propertyValue"></param>
public static void ReflectSetter(object obj, string propertyName, object propertyValue)
{
var type = obj.GetType();
var propertyInfo = type.GetProperty(propertyName);
propertyInfo.SetValue(obj, propertyValue);
}
/// <summary>
/// 表达式获取对象的属性值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propertyName"></param>
/// <returns></returns>