简体   繁体   English

为什么`Assembly`和`Module`没有公开定义的构造函数?

[英]Why do `Assembly` and `Module` have no publicly defined constructors?

I'm building a .NET assembly loader in C# for an "experiment"/learning more about .NET internal operations. 我正在C#中构建.NET程序集加载器,以进行“实验” /了解有关.NET内部操作的更多信息。 I've implemented the reflection API by deriving types like: 我已经通过派生以下类型来实现了反射API:

  • RuntimeType : Type RuntimeType:类型
  • RuntimeFieldInfo : FieldInfo RuntimeFieldInfo:FieldInfo
  • RuntimeMethodInfo : MethodInfo RuntimeMethodInfo:MethodInfo
  • RuntimeParameterInfo : ParameterInfo RuntimeParameterInfo:ParameterInfo
  • RuntimeConstructorInfo : ConstructorInfo RuntimeConstructorInfo:ConstructorInfo
  • RuntimePropertyInfo : PropertyInfo RuntimePropertyInfo:PropertyInfo

Unfortunately I'm having trouble because the following have no publicly accessible constructors, so I can't derive from them: 不幸的是,我遇到了麻烦,因为以下没有可公开访问的构造函数,因此我不能从中派生它们:

  • Assembly (not sealed - AssemblyBuilder is internally derived from it) 程序集(未密封-AssemblyBuilder是从内部派生的)
  • Module (not sealed - ModuleBuilder is internally derived from it) 模块(未密封-ModuleBuilder从内部派生)

I need something to return from RuntimeType.get_Assembly and RuntimeType.get_Module . 我需要一些东西从RuntimeType.get_AssemblyRuntimeType.get_Module返回。 Suggestions? 建议? Get creative - I've had to. 发挥创意-我必须这样做。 ;) For creating RuntimeTypeHandle instances, some unsafe casting of pointers gets the job done, but it's not so easy here. ;)对于创建RuntimeTypeHandle实例,一些不安全的指针转换可以完成任务,但是在这里并不是那么容易。

As an aside: The particular trouble now is I'm trying to pretty-print the IL for the loaded types. 顺便说一句:现在特别麻烦的是我正在尝试为加载的类型漂亮地打印IL。 For constructed generics, the Type.FullName property includes the generic parameters via the AssemblyQualifiedName property, which in turn relies on the Assembly property. 对于构造的泛型, Type.FullName属性通过AssemblyQualifiedName属性包括泛型参数,而该属性又依赖于Assembly属性。

Why can't you derive from them? 你为什么不能从他们那里得到? Both of the types (Assembly and Module) have a protected constructor that will be available to your derived type. 两种类型(Assembly和Module)都具有受保护的构造函数,该构造函数可用于您的派生类型。 The types themselves are public so there is no issue with it being less visible than the actual specifier. 这些类型本身是公共的,因此与实际的说明符相比,其可见性没有问题。

The following code compiles just fine 以下代码可以编译

public class MyAssembly : System.Reflection.Assembly {
    public MyAssembly() : base() {}
}

As to why they don't have any public constructors. 至于为什么他们没有任何公共构造函数。 I don't have any direct knowledge of this but it seems clear they wanted to force people to derive from these classes. 我对此没有任何直接了解,但显然他们想强迫人们从这些课程中汲取教训。 It does seem odd considering they have no abstract members. 考虑到他们没有抽象成员,这似乎很奇怪。 The only thing I can think of is a versioning issue but no logic behind that is jumping to my mind right now. 我唯一能想到的是版本问题,但目前尚无任何背后的逻辑。

EDIT 编辑

After I read 280Z28's comment I checked and yes this is new to .Net 4.0. 在阅读280Z28的注释后,我检查了,是的,这是.Net 4.0的新功能。 Prior to 4.0 the constructor for both Assembly and Module was internal and both types were concrete but not sealed. 在4.0之前的版本中,Assembly和Module的构造函数都是内部的,并且两种类型都是混凝土的,但不是密封的。 So derivation prior to 4.0 is not possible without doing some evilness with fully trusted code. 因此,如果不对完全受信任的代码进行一些恶意操作,则无法进行4.0之前的派生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM