using System.Reflection;
public class Dynamic {
/// <summary>
/// 设置字段值
/// </summary>
/// <param name="obj"></param>
/// <param name="fieldName"></param>
/// <param name="value"></param>
public static void SetFieldValue(object obj , string fieldName , object value) {
FieldInfo info = obj.GetType().GetField(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
info.SetValue(obj , value);
}
/// <summary>
/// 设置字段值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="fieldName"></param>
/// <param name="value"></param>
public static void SetFieldValue<T>(object obj , string fieldName , T value) {
FieldInfo info = obj.GetType().GetField(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
info.SetValue(obj , value);
}
/// <summary>
/// 设置属性值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="fieldName"></param>
/// <param name="value"></param>
public static void SetPropertyValue(object obj , string fieldName , object value) {
PropertyInfo info = obj.GetType().GetProperty(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
info.SetValue(obj , value);
}
/// <summary>
/// 设置属性值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="fieldName"></param>
/// <param name="value"></param>
public static void SetPropertyValue<T>(object obj , string fieldName , T value) {
PropertyInfo info = obj.GetType().GetProperty(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
info.SetValue(obj , value);
}
/// <summary>
/// 字段取值
/// </summary>
/// <param name="obj"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static object GetFieldValue(object obj , string fieldName) {
FieldInfo info = obj.GetType().GetField(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
return info.GetValue(obj);
}
/// <summary>
/// 字段取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static T GetFieldValue<T>(object obj , string fieldName) {
FieldInfo info = obj.GetType().GetField(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
return (T)info.GetValue(obj);
}
/// <summary>
/// 获取属性值
/// </summary>
/// <param name="obj"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static object GetPropertyValue(object obj , string fieldName) {
PropertyInfo info = obj.GetType().GetProperty(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
return info.GetValue(obj);
}
/// <summary>
/// 获取属性值
/// </summary>
/// <param name="obj"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static T GetPropertyValue<T>(object obj , string fieldName) {
PropertyInfo info = obj.GetType().GetProperty(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
return (T)info.GetValue(obj);
}
/// <summary>
/// 获取方法
/// </summary>
/// <param name="obj"></param>
/// <param name="methodName"></param>
/// <returns></returns>
public static MethodInfo GetMethodInfo(object obj , string methodName) {
MethodInfo info = obj.GetType().GetMethod(methodName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
return info;
}
}
C#反射 获取设置字段、属性值以及方法调用
最新推荐文章于 2023-04-24 22:12:11 发布