Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据反射,一个.NET对象实际上可以没有构造函数吗?_.net_Reflection - Fatal编程技术网

根据反射,一个.NET对象实际上可以没有构造函数吗?

根据反射,一个.NET对象实际上可以没有构造函数吗?,.net,reflection,.net,Reflection,我正在编写一个非常简单的IoC/DI容器,我有以下代码: ConstructorInfo[] ctors = concreteType.GetConstructors(); if (ctors.Length == 0) return Activator.CreateInstance(concreteType); // more code goes here... 我无法想出一个导致类型具有零构造函数的测试用例,即使是这样: class LonelyType {} .NET类型是否

我正在编写一个非常简单的IoC/DI容器,我有以下代码:

ConstructorInfo[] ctors = concreteType.GetConstructors();

if (ctors.Length == 0)
    return Activator.CreateInstance(concreteType);

// more code goes here...
我无法想出一个导致类型具有零构造函数的测试用例,即使是这样:

class LonelyType {}

.NET类型是否可能没有构造函数,或者CLR是否始终提供隐式构造函数?

是-静态类没有构造函数:

static class VeryLonelyType{}
正常的无参数构造函数由C#编译器提供,而不是CLR

注意,就CLR而言,从C#生成的结构也没有无参数构造函数。例如:

struct Foo{}
将不包含无参数构造函数。你可以在IL做,它可以