如何获取 class 成员(字段和属性)及其值的列表?
How to get a list of the class members (fields and properties) and their values?
我找到了一个解决方案,但它似乎不适合我(
(CrazyCasta 回答):
how to get both fields and properties in single call via reflection?
正如我在使用 CrazyCasta 的代码之前所说:
interface IGetSettable
{
public void SetValue(
Object obj,
Object value,
Object[] index);
public Object GetValue(
Object obj,
Object[] index);
}
public class ParameterInfoGS : IGetSettable
{
protected ParameterInfo pi;
public ParameterInfoExtra(ParameterInfo _pi)
{
pi = _pi;
}
public void SetValue(
Object obj,
Object value,
Object[] index) { pi.SetValue(obj, value, index); }
public Object GetValue(
Object obj,
Object[] index) { return pi.GetValue(obj, index); }
}
public class FieldInfoGS : IGetSettable
{
protected FieldInfo pi;
public FieldInfoExtra(FieldInfo _pi)
{
pi = _pi;
}
public void SetValue(
Object obj,
Object value,
Object[] index) {pi.SetValue(obj, value, index);}
public Object GetValue(
Object obj,
Object[] index) {return pi.GetValue(obj, index);}
}
public static class AssemblyExtension
{
public static IGetSettable[] GetParametersAndFields(this Type t)
{
List<IGetSettable> retList = new List<IGetSettable>();
foreach(ParameterInfo pi in t.GetParameters())
retList.Add(new ParameterInfoExtra(pi));
foreach(FieldInfo fi in t.GetFields())
retList.Add(new FieldInfoExtra(fi));
return retList.ToArray();
}
}
我遇到了很多错误,例如 ParameterInfo 不包含任何 GetValue SetValue 方法
我在之前的项目中做过类似的事情。看看那个:
IEnumerable<PropertyInfo> props = from n in typeof(MyClass).GetProperties()
let get = n.GetGetMethod()
let set = n.GetSetMethod()
where get != null && set != null &&
n.DeclaringType == typeof(MyClass)
select n;
foreach (PropertyInfo info in props)
{
// Code goes here. Something like:
object obj = info.GetValue(instanceOfMyClass);
}
但最终我摆脱了那个代码,因为这是一个糟糕的方法;)
我找到了一个解决方案,但它似乎不适合我( (CrazyCasta 回答):
how to get both fields and properties in single call via reflection?
正如我在使用 CrazyCasta 的代码之前所说:
interface IGetSettable
{
public void SetValue(
Object obj,
Object value,
Object[] index);
public Object GetValue(
Object obj,
Object[] index);
}
public class ParameterInfoGS : IGetSettable
{
protected ParameterInfo pi;
public ParameterInfoExtra(ParameterInfo _pi)
{
pi = _pi;
}
public void SetValue(
Object obj,
Object value,
Object[] index) { pi.SetValue(obj, value, index); }
public Object GetValue(
Object obj,
Object[] index) { return pi.GetValue(obj, index); }
}
public class FieldInfoGS : IGetSettable
{
protected FieldInfo pi;
public FieldInfoExtra(FieldInfo _pi)
{
pi = _pi;
}
public void SetValue(
Object obj,
Object value,
Object[] index) {pi.SetValue(obj, value, index);}
public Object GetValue(
Object obj,
Object[] index) {return pi.GetValue(obj, index);}
}
public static class AssemblyExtension
{
public static IGetSettable[] GetParametersAndFields(this Type t)
{
List<IGetSettable> retList = new List<IGetSettable>();
foreach(ParameterInfo pi in t.GetParameters())
retList.Add(new ParameterInfoExtra(pi));
foreach(FieldInfo fi in t.GetFields())
retList.Add(new FieldInfoExtra(fi));
return retList.ToArray();
}
}
我遇到了很多错误,例如 ParameterInfo 不包含任何 GetValue SetValue 方法
我在之前的项目中做过类似的事情。看看那个:
IEnumerable<PropertyInfo> props = from n in typeof(MyClass).GetProperties()
let get = n.GetGetMethod()
let set = n.GetSetMethod()
where get != null && set != null &&
n.DeclaringType == typeof(MyClass)
select n;
foreach (PropertyInfo info in props)
{
// Code goes here. Something like:
object obj = info.GetValue(instanceOfMyClass);
}
但最终我摆脱了那个代码,因为这是一个糟糕的方法;)