Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference from string variable

Status
Not open for further replies.

TimSmithSK

Programmer
Jun 12, 2009
2
US
Given a class declaration such as:

public class MyClass
{
public System.String prop1 { get; set; }
public System.String prop2 { get; set; }
}

and code like:

MyClass c = new MyClass();

c.prop1 = "Value of property 1";
c.prop2 = "Value of property 2";

System.String s = "MyClass.prop1";

Is there a way to use the value of the string variable s to get the value of the MyClass.prop1 property - something along the lines of GetObject("MyClass").GetProperty("prop1") -- where the result would be the "Value of property 1".

Thanks
Tim
 
using reflection you can do this. however it's not recommended. what are you trying to do that you need a fully dynamic accessor?

reflection would look something like this.
Code:
var c = new MyClass()
{
   prop1 = "Value of property 1",
   prop2 = "Value of property 2"
};

var value = (string)Type
   .GetType("name.space.MyClass")
   .GetProperty("prop1")
   .GetValue(c, null);



Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Reflection can be a bit slow in C#, at least for initial calls until the internal cache is built up.

Having a "get by name" method is a feature in other languages, like Objective C

Chip H.


____________________________________________________________________
www.chipholland.com
 
Jason/Chip -

Thanks for the replies. The reasoning behind this idea is that templates within the application can contain string representations of instantiated objects and can thus translate them on the fly. (Jason - that is why, although your code works in general, it will not work for me because it still needs a references to a previously created object, which I cannot simply create when calling this function).

In the end, I created a dictionary object whose key is a string reference to the object (in this case, "MyClass") and the value is the object itself. I can get the object from the dictionary by name and use reflection to get the desired property.

Thanks again
Tim
 
Tim, I would rethink your approach. dictionaries work when encapsulated, but publicly exposed dictionaries (especially string dictionaries) are a disaster waiting to happen.

Why not use basic OOP encapsulation techniques to abstract the details of the implementation? Adapter and Command patterns come to mind. This will create more code, but the code is compile time checked and you can test them using an automated testing framework like nUnit or MbUnit.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top