Finding Object types and Properties at runtime

I've declared a class Shape with 4 Properties all of type String and named them FirstSide, SecondSide, ThirdSide, and FourthSide.

I also have a static helper class with a method:

Public static void Populate(Object inputObject)

This method can take different classes as arguments at runtime and then set each of the Properties of the underlying class. So I pass:

Shape myShape = New Shape;

Helper.Populate(myShape);

Within the Helper class I want to do:

console.writeline(inputObject.Type); //correct

inputObject.FirstSide = "Longest"; //how do I do this bit

inputObject.SecodnSide = "Shortest";

...etc

How can I do this if I don't know the name of the object properties until runtime Presumably I can change the syntax to do this somehow or use some NET framework class. I will be using a config file at runtime so I can tell which named Properties belong to which class. I also need to know which class I've passed-in at runtime.




Answer this question

Finding Object types and Properties at runtime

  • Suresh C

    joeycalisay wrote:
    use GetType() to determine it's type, and use Reflection to set the value an instance's properties given their property names.

    Isn't the C# standard typeof() I thought GetType() was VB-standard.



  • jfalberg

    Good to know ! I always use(-d) typeof() in C# and GetType() in VB.

  • robinsenior

    use GetType() to determine it's type, and use Reflection to set the value an instance's properties given their property names.


  • J. Shen

    My first question would be: why not declare your Populate method as taking a Shape rather than an Object (Or use either interfaces or inheritance if you have lots of different kinds of shapes.) But assuming you have a good answer for that, here are a couple of ways you can do it.

    The simplest:

    Type t = inputObject.GetType();

    t.InvokeMember("FirstSide", BindingFlags.SetProperty, null, inputObject, "Longest");

    There are some refinements you could use. This will only work if the data type you pass in as the value is an exact match for the data type of the property. There are things you can do to manage coercion in cases where you can't guarantee that.

    This will also do a name-based lookup every time you do it. In some cases it may make sense to do this lookup just once:

    PropertyInfo pi = inputObject.GetType().GetProperty("FirstSide");

    And then you can use this PropertyInfo to set the property:

    pi.SetValue(inputObject, "Longest");

    The call to SetValue will be a bit faster than the one to InvokeMember because it doesn't have to pay the cost of looking up the property by name. You paid that cost when you called GetProperty earlier.

    Of course this is only any use if you're repeatedly working with the same object, or objects all of the same type. So this would be worthwhile if you had an array of objects, all the same type, and wanted to access the same property on every object in the array. But for a one-off approach, which is what it looks like you're doing, the simpler approach is probably best. (The more complex approach wouldn't actually offer any perf benefits here.)

    The other thing that using PropertyInfo offers you is that it can tell you things about the property - e.g. its type, and whether the property accessors take parameters.


  • Mweimer

    Reinout Waelput wrote:

    joeycalisay wrote:
    use GetType() to determine it's type, and use Reflection to set the value an instance's properties given their property names.

    Isn't the C# standard typeof() I thought GetType() was VB-standard.


    GetType() is part of the Object class of the framework (i don't think it's a vb-standard), you can use either in c#.


  • gotit

    So how do I "use Reflection"

  • Finding Object types and Properties at runtime