有没有一种方法可以使用其组成部分的字符串表示来构造对象,而无需在此特定步骤中使用任何反射?
假设我有一些 MyObject obj
对象,并且想要访问该对象的属性,例如 obj.SomeCollection.SomeProperty
。是否可以通过组合 obj 和表示为字符串的 .SomeCollection.SomeProperty 来获取此对象的该属性,而不调用此特定步骤的任何反射函数?
更新:我试图避免反射的原因是因为性能受到巨大影响。基本上,我将使用反射来提取其组成部分的字符串表示,并将它们存储在某种字典中,从那时起,该字典将被重用,从而避免每次需要访问对象的属性时都使用反射。我之所以如此担心性能,是因为我需要使用此流程(如果存在)进行企业级系统单元测试。通过反思,这种用法变得不切实际,因为运行这些测试可能需要长达一个小时。希望这能解释我的立场。
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 take up to an hour to run these tests. Hope this explains my position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我倾向于同意@Scott(为什么不使用反射,非常适合这个......)如果您可以将对象序列化为XML,甚至更好的XML数据集,您可以使用各种选项来与字符串协商对象作为标识符。
以下编辑发布了 @Ukraine Train 2 条关于性能的评论
@Ukraine Train,反射和 XML 序列化都不是真正的性能狗。 “性能”取决于您想要处理的迭代次数以及处理它们的速度。一旦您通过反射或 XML 序列化完成对象的实例化,该对象就会“在内存中”,并且所有访问都将通过指针进行。你想一次在内存中驻留多少个对象,你希望它们存活多长时间,这些都是战略决策,我没有足够的信息来为你提供建议。
如果您希望能够使用字符串标识符询问对象,您的选择几乎就是反射或序列化。我相信当您称反射为“巨大”时,您高估了反射对性能的影响。你能最快完成的事情是什么都没有,其他任何事情都需要时间和资源。
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.
您可以使用 C# 4 中新增的
dynamic
关键字。这允许您使用编译时未知的对象的方法和属性。您可以在 MSDN 此处 上了解它,但基本用法是:只要实例方法存在并且可以在运行时解析,它们就会成功执行。如果它们不存在,则会抛出运行时异常。
这不会按照您的要求使用对象方法的字符串表示形式,但无论如何它可能值得考虑。就性能而言,这应该与所描述的其他方法相比较。但是,您应该分析您的应用程序以验证您选择的方法。
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: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.