C# 从共享/pcl项目中的对象获取具有反射的属性
我有一个500个参数的对象,我不能更改它,因为它是一个共享项目。 我的目标是通过反射获得属性值,但我不能这样做 这是我的密码:C# 从共享/pcl项目中的对象获取具有反射的属性,c#,reflection,C#,Reflection,我有一个500个参数的对象,我不能更改它,因为它是一个共享项目。 我的目标是通过反射获得属性值,但我不能这样做 这是我的密码: public class MyObjects { public int RGP_Id { get; set; } public DateTime RGP_DateTime { get; set; } public int RGP_MCC_Numero_Serie_MS { get; set; } public int RG
public class MyObjects
{
public int RGP_Id { get; set; }
public DateTime RGP_DateTime { get; set; }
public int RGP_MCC_Numero_Serie_MS { get; set; }
public int RGP_IDREG_1 { get; set; }
public int RGP_IDREG_2 { get; set; }
public int RGP_IDREG_3 { get; set; }
public int RGP_IDREG_4 { get; set; }
public int RGP_IDREG_5 { get; set; }
public int RGP_IDREG_6 { get; set; }
public int RGP_IDREG_7 { get; set; }
public int RGP_IDREG_8 { get; set; }
public int RGP_IDREG_9 { get; set; }
public int RGP_IDREG_10 { get; set; }
.......
public int RGP_IDREG_500 { get; set; }
}
idProperty包含该对象,我在上面做了
现在,如何使用idProperty获取值?
基本上我会得到:
var x = MyObjects.idProperty;
如何完成此操作?您可以使用以下代码还原值:
var x = idProperty.GetValue(profile, null))
您可以在此处获得更多信息:
假设
idProperty
是PropertyInfo
对象,您可以执行以下操作:
var result = (int) idProperty.GetValue(profile,null);
您必须确保强制转换该值,因为它是作为对象返回的
var result = (int) idProperty.GetValue(profile,null);