Activator.CreateInstance 的目的举例? [英] Purpose of Activator.CreateInstance with example?

查看:33
本文介绍了Activator.CreateInstance 的目的举例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能详细解释一下Activator.CreateInstance()的用途?

Can someone explain Activator.CreateInstance() purpose in detail?

推荐答案

假设您有一个名为 MyFancyObject 的类,如下所示:

Say you have a class called MyFancyObject like this one below:

class MyFancyObject
{
 public int A { get;set;}
}

它让你转身:

String ClassName = "MyFancyObject";

进入

MyFancyObject obj;

使用

obj = (MyFancyObject)Activator.CreateInstance("MyAssembly", ClassName))

然后可以执行以下操作:

and can then do stuff like:

obj.A = 100;

这就是它的目的.它还具有许多其他重载,例如提供 Type 而不是字符串中的类名.为什么你会遇到这样的问题是另一回事.以下是一些需要它的人:

That's its purpose. It also has many other overloads such as providing a Type instead of the class name in a string. Why you would have a problem like that is a different story. Here's some people who needed it:

这篇关于Activator.CreateInstance 的目的举例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆