Activator.CreateInstance(类型)的类型无参数的构造函数 [英] Activator.CreateInstance(Type) for a type without parameterless constructor

查看:1650
本文介绍了Activator.CreateInstance(类型)的类型无参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读在工作中现有的代码,我不知道怎么来的,这可能工作。我有一个程序集中定义一个类:

  [Serializable接口] 
公共A级
{
私人只读字符串_name;
私人A(字符串名称)
{
_name =名称;
}
}

和另一个程序:

 公共无效F(T型){
对象o = Activator.CreateInstance(T);
}

和简单的呼叫 F(typeof运算(A) )



我预想的是,没有一个参数的构造函数的异常,因为据我所知,如果一个构造函数声明,编译器不应该生成默认的公共参数构造函数。



这代码在.NET 2.0中运行。



我'对不起,但我误读了实际的代码......我提供的并没有说明它的样本。我接受了JonH答案,因为它提供了一个很好的资料片


解决方案

请参阅本:的http://stackoverflow.com/questions/390578/creating-instance-of-type



下面是对未来也-without默认的构造函数,在C-使用反射,这是关于C#4.0:




发布Microsoft在1/4/2010在
日下午2:08结果
我们已经审核了您的错误和
已经确定,b中描述了$ b $的行为是由设计。我们现在
存档这个问题。感谢您使用
Visual Studio和.Net框架!




有Activator.CreateInstance的大量重载,就是那个
拍摄单一类型的参数仅
调用默认的无参数
构造。称取像在
你的例子不是默认
构造一个
可选参数的构造函数。要调用都需要
为:




  1. 使用的重载接受一个参数数组

  2. 在Type.Missing作为参数传递

  3. 在指定的BindingFlags OptionalParamBinding



下面有一个例子:

  Activator.CreateInstance(typeof运算(MyClassName),
BindingFlags.CreateInstance |
的BindingFlags 。公共|
BindingFlags.Instance |
BindingFlags.OptionalParamBinding,
空,新的对象[] {} Type.Missing,NULL);



谢谢,



伟涛苏



微软公司。




Reading existing code at work, I wondered how come this could work. I have a class defined in an assembly :

[Serializable]
public class A
{
    private readonly string _name;
    private A(string name)
    {
        _name = name;
    }
}

And in another assembly :

public void f(Type t) {
    object o = Activator.CreateInstance(t);
}

and that simple call f(typeof(A))

I expected an exception about the lack of a parameterless constructor because AFAIK, if a ctor is declared, the compiler isn't supposed to generate the default public parameterless constructor.

This code runs under .NET 2.0.

[EDIT] I'm sorry but I misread the actual code... The sample I provided doesn't illustrate it. I accepted JonH answer because it provided a good piece of information.

解决方案

See this: http://stackoverflow.com/questions/390578/creating-instance-of-type-without-default-constructor-in-c-using-reflection

Here's to the future also, this is in regards to C# 4.0:

Posted by Microsoft on 1/4/2010 at 2:08 PM
We have reviewed your bug and have determined that the behavior that you described is by design. We are now archiving this issue. Thanks for using Visual Studio and the .Net Framework!

There are many overloads of Activator.CreateInstance, the one that takes a single type parameter only invokes the default parameterless constructor. Constructors that take an optional parameter like the one in your example are not default constructors. To invoke them you need to:

  1. use an overload that takes an argument array
  2. Pass in Type.Missing as the argument
  3. Specify OptionalParamBinding in the BindingFlags

Here is an example:

Activator.CreateInstance(typeof(MyClassName),
 BindingFlags.CreateInstance |
 BindingFlags.Public |
 BindingFlags.Instance |
 BindingFlags.OptionalParamBinding,
 null, new Object[] {Type.Missing}, null);

Thanks,

Weitao Su

Microsoft Corp.

这篇关于Activator.CreateInstance(类型)的类型无参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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