Mr. Developer Mr. Developer -4 years ago 142
C# Question

get properties with reflection from an object in shared/pcl project

I have an object with 500 parameters and I can not change it, because it is a shared project.
My goal is to get the property value through reflection, but I can not do it.

here is my code:

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; }

}


...

var profile = MyObjects;
var idProperty = GetProperty(profile.GetType().GetTypeInfo(), "RGP_IDREG_10");


...

idProperty contains the object, and up there I did.

Now, how can I obtain the value using idProperty?
basically I would get:

var x = MyObjects.idProperty;


How can i get this done?

Answer Source

You can restore value by using this code:

var x = idProperty.GetValue(profile, null))

You can get more information here: https://msdn.microsoft.com/cs-cz/library/b05d59ty(v=vs.110).aspx

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download