Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从共享/pcl项目中的对象获取具有反射的属性_C#_Reflection - Fatal编程技术网

C# 从共享/pcl项目中的对象获取具有反射的属性

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

我有一个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 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);