0

I have an object of 500 properties, my need is to call the object dynamically using string. How can I do using c #?

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

}

I would need to call the object's properties and return its value, using string.

target example:

var x = MyObjects.GetPropertyValue("RGP_IDREG_10"); 

it's possible ?

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
  • The demonstrated goal isn't really clear to me. You want a `static` method called `Equals()` which takes a `string` and does what exactly? What would `x` be after this operation? – David Feb 28 '17 at 13:40
  • I would need to call the object's properties and return its value, through the string ... – Mr. Developer Feb 28 '17 at 13:42
  • it's not a duplicate @David this is for xamarin forms....GetProperty() not exist in xamarin forms c# – Mr. Developer Feb 28 '17 at 13:46
  • Interesting. Can you elaborate on how you've attempted to use that solution and in what way it failed? – David Feb 28 '17 at 13:47
  • I am trying with the GetType () but does not have methods that can retrieve a value by string, the reflection I can not use it because the .net xaamrin forms does not support this feature fully. – Mr. Developer Feb 28 '17 at 13:50
  • @Mr. Developer Did you think about using a `List` instead of 500 properties? – Dennis Schröer Feb 28 '17 at 13:53
  • Yes I thought about list, but this project is shared project and I can't change the object.. – Mr. Developer Feb 28 '17 at 13:55
  • getting the property will require reflection, and reflection can be slow, esp. on mobile devices – Stephane Delcroix Mar 01 '17 at 08:19

3 Answers3

0

You could use something like this question to get the property list and then access the property. You will need to use System.Reflection but it should be available to you.

Community
  • 1
  • 1
nkorai
  • 145
  • 1
  • 11
0

I would recommend using anything else than this, because it require reflection, and reflection might be slow, but if you really want to do it...

using System.Reflection;

...

var objects = new MyObjects();
var propertyInfo = typeof(MyObjects).GetRuntimeProperty("RGP_IDREG_1");
var value = (int)(propertyInfo.GetMethod.Invoke(objects, new object [] {}));
Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
-1

SOLUTION in Xamarin Forms

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

idProperty contain value from property

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110