okay, I'm trying to reference a property within a class using a string "class.property ", I've got a test class, imaginitively called "Test", hold your applause...it's got a property called value.
I've got a function called GetValue that accepts an array of objects and a string for the property reference I want to get the value of.
Now I can use reflection to create a type and use the GetProperty method to reference the "value" property:
This gets a dynamic object of type "Test":
at which point, I can use MyClass.GetProp erty("Value") to grab the property's value and return it...seems simple enough.
Okay, lets add some complexity. Given that I can pass an array of objects into my function, I want to be able to give a fully qualified property name to reference:
Lets say there's two classes, I want to specify which class to pass the value back from:
I think I could do this readily enough if I could pass a string value into GetType (which obviously I can't)... rather than code a switch/case to pass the correct type given a string, is there an easy way to evaluate a string as the type it refers to rather than as a string?
(Apologies for not using code tags, the new code windows are a touch excessive for small code blocks)
I've got a function called GetValue that accepts an array of objects and a string for the property reference I want to get the value of.
Now I can use reflection to create a type and use the GetProperty method to reference the "value" property:
This gets a dynamic object of type "Test":
Dim MyClass As Type = GetType(Test)
at which point, I can use MyClass.GetProp erty("Value") to grab the property's value and return it...seems simple enough.
Okay, lets add some complexity. Given that I can pass an array of objects into my function, I want to be able to give a fully qualified property name to reference:
Lets say there's two classes, I want to specify which class to pass the value back from:
Dim Args(1) As Object
Args(0) = New Test1(123.45)
Args(1) = New Test2(987.65)
Dim Value = GetValue(Args, "Test1.Valu e")
Args(0) = New Test1(123.45)
Args(1) = New Test2(987.65)
Dim Value = GetValue(Args, "Test1.Valu e")
I think I could do this readily enough if I could pass a string value into GetType (which obviously I can't)... rather than code a switch/case to pass the correct type given a string, is there an easy way to evaluate a string as the type it refers to rather than as a string?
(Apologies for not using code tags, the new code windows are a touch excessive for small code blocks)
Comment