根据反射,一个.NET对象实际上可以没有构造函数吗?
我正在编写一个非常简单的IoC/DI容器,我有以下代码:根据反射,一个.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类型是否
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做,它可以