Is there a way to construct an object using a string representation of its constituent parts without using any reflection for this particular step?
Let's assume I have some MyObject obj
object and want to access a property of that object such as obj.SomeCollection.SomeProperty
. Is it possible to get that property on this object by combining obj and .SomeCollection.SomeProperty represented as a string without invoking any of the reflection functions for this particular step?
UPDATE: The reason why I'm trying to avoid reflection is because of a huge performance hit. Basically, I will use reflection in order to extract the string reperesentation of its constituent parts and store them in some kind of dictionary which will be reused from that point on, thus avoiding using reflection every time I need an access to object's properties. The reason why I'm so worried about performance is because I need to use this process, if it exists, for enterprise-level system unit tests. With reflection this usage becomes unrealistic as it could t开发者_运维百科ake up to an hour to run these tests. Hope this explains my position.
While I tend to agree with @Scott (why not use Reflection, perfect for this...) if you can serialize the object to XML, or even better an XML DataSet, you can use a variety of options to negotiate the object with strings as identifiers.
Following edit posted following @Ukraine Train 2 comments re: performance
@Ukraine Train, Neither reflection nor XML serialization are real performance dogs. "Performance" depends on how many iterations you are wanting to process and how fast you want to process them. Once you go through the instantiation of the object through reflection or XML serialization the object is then 'in memory' and all access will be via pointers. How many objects you want to reside in memory at a time, how long you want them to live are all strategic decisions I don't have enough information to advise you on.
If you want to be able to interrogate the object using string identifiers, your choices are pretty much reflection or serialization. I believe you are overestimating the performance hit of reflection when you call it 'huge'. The fastest thing you can accomplish is nothing, anything else takes time and resources.
You may be able to use the dynamic
keyword, new in C# 4. This allows you to use methods and properties of an object not known at compile-time. You can read about it on MSDN here, but the basic usage is:
public static void Main()
{
// Instantiate or retrieve your object into memory
dynamic obj = RetrieveCustomObject();
// Invoke methods on object not known at compile time
int id = obj.Id;
bool result = obj.DoWork();
}
As long as the instance methods exist and can be resolved at runtime time, they will execute successfully. If they don't exist, a runtime exception will be thrown.
This doesn't use string representations of the object's methods as you requested, but it may be worth considering anyway. In terms of performance, this should compare well with other methods described. However, you should profile your application to validate whichever method you choose.
精彩评论