- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Activator.CreateInstance 动态创建一个实例。但是,它说对象在每次尝试时都不能为空。粘贴下面的代码。我做错了什么吗?
有什么问题吗
Activator.CreateInstance
取代传统的 switch/case 语句来确定运行时的对象类型?谢谢。
public abstract class Base
{
public abstract void Func();
}
public class Derived:Base
{
public override void Func()
{
MessageBox.Show("Derived First");
}
}
public class Derived2 : Base
{
public override void Func()
{
MessageBox.Show("Derived Second");
}
}
private void button1_Click(object sender, EventArgs e)
{
// I was trying to make use of the overladed version
// where it takes the Type as parameter.
BaseClass report =
(BaseClass) Activator.CreateInstance(Type.GetType("Derived"));
report.Func();
}
最佳答案
来自 Type.GetType
的 typeName
参数的文档:
The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.
这意味着您(至少)还需要传递命名空间:
BaseClass report = (BaseClass) Activator.CreateInstance(Type.GetType("YourNamespace.Derived"));
关于c# - Activator.CreateInstance 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8969268/
这些调用有什么区别? 最佳答案 没有。 Assembly.CreateInstance 实际上在幕后调用了 Activator.CreateInstance。 在 Assembly.CreateIns
1)如果我们想在运行时创建给定类型的实例(因此使用后期绑定(bind)),那么我们需要调用 Activator.CreateInstance。但是如果Type类有这样的方法不是更高效吗?如果不出意外,
我想问一个问题来了解AppDomain和Activator之间的区别,我通过appdomain.CreateInstance加载了我的dll。但我意识到更多的方法来创建实例。因此,我何时何地选择这种方
这些调用有什么区别? 最佳答案 没有。 Assembly.CreateInstance 实际上在幕后调用了 Activator.CreateInstance。 在 Assembly.CreateIns
不,这不是关于泛型的问题。 我有一个工厂模式,其中包含几个带有内部构造函数的类(如果不通过工厂,我不希望它们被实例化)。 我的问题是 CreateInstance 失败并出现“没有为此对象定义无参数构
如果这个问题已经被问过和回答过,请原谅我。 给定一个类型为 T 的类,下面的区别是什么? T myObj = Activator.CreateInstance(); T myObj = typeof(
我正在尝试将插件系统与 .NET 放在一起,但我不确定我是否正确地做。系统的基础是一个特定的目录({apppath}/Plugins/)会有一堆预编译的DLL,我想用反射查看每一个,对于每个可用的类,
我需要获取 DataContext 的子类,我在某处找到了下面的函数,它确实找到了我的子类,但我无法实例化它:( public static IEnumerable GetSubclassesFo
这是我的构造函数: CMSATools::CMSATools() { m_pInterface = NULL; HRESULT hr; hr = m_pInterface.Cr
当我们使用 AppDomain.CreateInstance("Assembly name", Type name) 我的类继承自 MarshalByRefObject 内部发生了什么?它是否创建了一
我做了以下创建接口(interface)实例的方法。 static IFBIndexItem* CreateFBIndexItemPtr() { IFBIndexItemPtr pFBComW
我一直在我的一些代码中使用 Activator.CreateInstance()。使用它创建实例有任何风险吗? 最佳答案 好吧,你的代码存在弱类型的风险,你不会发现你不小心尝试将它与一个直到执行时才没
我想我缺乏了解,究竟发生了什么: 用户可以输入程序集的路径和对象类型,然后我尝试创建它的实例。 我的做法: Assembly a = Assembly.LoadFile(txtAsse
什么是System.Activator.CreateInstance,什么时候应该使用它? 最佳答案 它允许您创建其类型仅在运行时已知的对象的实例。因此,假设您有一些类(class) public c
有谁知道如何使用只有 1 个可选参数的构造函数来实例化一个类? 我都试过了 (T)Activator.CreateInstance(typeof(T), new object[] { Type.Mis
我们有一些使用 MSXML 的代码,这样做是为了创建 XML 文档对象: MSXML2::IXMLDOMDocumentPtr doc_in; doc_in.CreateInstance("Msxm
使用这段代码: public static class ChocolateFactory { private static Func Func { get; set; } static
我想将变量从一种形式传递到另一种形式。 这是 form2 中的构造函数: public Form2 (int getId,string getText) 在 form1 中我试图像这样传递变量 var
Activator.CreateInstance 和工厂有什么区别?它们可以互换使用吗?或者我们还需要工厂模式吗? 最佳答案 Activator.CreateInstance是一个静态方法,它使用该类
拥有Remember.cs: namespace Tasks { public class Remember : Task { new public string na
我是一名优秀的程序员,十分优秀!