0

I have different objects like: CallingItem, TransferItem or FlowItem and I have an appropriated String containing the needed property of the object e.g. Message.Start.MsgNumber or Data.Name

Examples of my objects with property:

TransferItem.Message.Start.MsgNumber = 5;
CallingItem.Data.Name = "Mike"

Method call:

GetProperty(TransferItem, "Message.Start.MsgNumber");
GetProperty(CallingItem, "Data.Name");  

Method GetProperty:

public void GetProperty(object myObject, string propertyString)
{
    Type t = myObject.GetType();

    PropertyInfo pinfo = t.GetProperty(propertyString);

    if (pinfo == null)
        return;

    object value = pinfo.GetValue(null, null);
}

But pinfo is always null. How can I get the property value of my object?

4
  • 2
    Why do you work with strings at all? Commented Sep 4, 2017 at 7:01
  • GetProperty does not support nesting properties like "Foo.Bar". Commented Sep 4, 2017 at 7:01
  • You need some recursive method that returns just a single part of your path instead of the entire structure. Commented Sep 4, 2017 at 7:05
  • 2
    In addition to the dupe, you can also look here for jonskeet answer:stackoverflow.com/questions/366332/… Commented Sep 4, 2017 at 7:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.