Type.GetType() 是否会根据您要检索的对象的大小和复杂性而减慢速度?
我有一个应用程序今天使用基本反射来获取课程。
Type type = Type.GetType(mynamespace.myclassname);
object o = System.Activator.CreateInstance(type);
我想看看反射运行的效率如何,因此我以这种方式生成了大约 150,000 个对象,以查看性能是否下降以及性能是否快速且稳定。
然而,这让我开始思考:对 Type.GetType() 的调用实际上会根据传递到 GetType() 方法的类的大小和复杂性而变慢吗?
例如:假设我们想要使用 GetType() 检索一个由 30 个私有变量、30 个私有方法和 30 个公共方法组成的复杂类,而不是一个只有一个非常简单的公共 Add(int, int) 方法的类,该方法求和增加两个数字。
如果传入的类是复杂类而不是简单类,Type.GetType 是否会显着减慢速度?
谢谢
I have an application that is using basic reflection today to grab classes.
Type type = Type.GetType(mynamespace.myclassname);
object o = System.Activator.CreateInstance(type);
I wanted to see how efficient the reflection was running so I generated about 150,000 objects in this manner to see if performance ever degraded and performance was fast and steady.
However, this got me thinking: Will the call to Type.GetType() actually slow down depending on the size and complexity of the class being passed into the GetType() method?
For example: Lets say we wanted to use GetType() to retrieve a complex class made up of 30 private variables, 30 private methods and 30 public methods versus a class that has only one very simple public Add(int, int) method which sums up two numbers.
Would Type.GetType slow down significantly if the class being passed in is a complex class versus a simple class?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我对事物的理解,(我只是一个谦虚的经验丰富的程序员,我不是该语言的创建者之一)所引用的类的复杂性不会以任何方式影响
GetType()
。实例化类的复杂性当然会影响
CreateInstance()
的性能,但这是可以预料的:类越大,包含的内容越多,需要的代码就越多执行以完全构建它。也许您将
GetType()
与CreateInstance()
混淆了,因为我注意到您说“在实例化复杂类与简单类时,Type.GetType 会显着减慢速度吗?”事实上,GetType()
不会实例化任何东西,而CreateInstance()
会实例化任何东西。According to my understanding of things, (and I am just a humble experienced programmer, I am not one of the creators of the language,) the complexity of the class which is being referred does not in any way whatsoever affect the performance of
GetType()
.The complexity of the instantiated class will of course affect the performance of
CreateInstance()
, but that is to be expected: the larger the class is, the more stuff it contains, the more code will need to be executed in order to fully construct it.Perhaps you are confusing
GetType()
withCreateInstance()
, because I notice you say "Would Type.GetType slow down significantly when instantiating the complex class versus the simple class?" while in factGetType()
does not instantiate anything,CreateInstance()
does.加载类型时会创建适合您的类型的 Type 对象实例。这一(一次性)过程显然会或多或少昂贵,具体取决于实施类型。
此后,对 GetType() 的调用将为您提供对此准备好的实例的引用,因此不会随着时间或复杂性而降低。
An instance of the Type object fitting to your type is created when the type is loaded. This (one-time) process will obviously be more or less expensive depending on the type implementation.
After this, a call to GetType() will give you a reference to this readily-prepared instance, and will thus not degarde over time or complexity.