0

this question has been asked here for js
how to access object property using variable
How to access a dynamic property: objectName.{variable}

however im looking how to do this for asp.net mvc4
the syntax for js gives me errors for .net

basically
trying to access a property of an object using a dynamic variable

so for example
if i have a var k="myProp" and i have an object o
this statement var v = o.k;
would return the value of o.myProp not o.k
(which would return an err if o does not have a property named "k")

Community
  • 1
  • 1
toy
  • 422
  • 1
  • 7
  • 19

1 Answers1

1

You will need to use Reflection, which means getting a Type object to represent the type of the object whose property value you want to get, getting a PropertyInfo for the property and then getting the value of that property for that object. There are plenty of examples around.

By the way, there's nothing "dynamic" about your variable. It's just a variable, i.e. its value can vary. A dynamic variable is one declared dynamic, which yours is not.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46