[英]Why isn't the class Type sealed, and what can I do with that?
I was looking at the metadata for Type, and noticed a member was protected, which made me wonder what might derive from it, which made me realize I could write a class that derives from it, and all that makes me wonder what I can do with that (if anything). 我正在查看Type的元数据,并注意到一个成员受到保护,这让我想知道它可能从中得到什么,这让我意识到我可以编写一个源自它的类,这一切让我想知道我能做些什么与此(如果有的话)。
No compiler error from following: 以下没有编译器错误:
class MyType : Type
{
// Implemented abstract members here.
}
Great question. 好问题。 I only have a partial answer.
我只有部分答案。 There are currently 4 classes that derive from Type.
目前有4个类派生自Type。 You can find the Type hierarchy on MSDN .
您可以在MSDN上找到类型层次结构。
System.Object
System.Reflection.MemberInfo
System.Type
System.Reflection.Emit.EnumBuilder
System.Reflection.Emit.GenericTypeParameterBuilder
System.Reflection.Emit.TypeBuilder
System.Reflection.TypeDelegator
It looks like those types are basically used to encapsulate some "instance-building" logic. 看起来这些类型基本上用于封装一些“实例构建”逻辑。 But I haven't explored the code.
但我没有探索过代码。
Edit 编辑
Oh, wow... that's interesting. 哦,哇......这很有趣。 The code examples seem to not only be creating instances of types, but also classes themselves.
代码示例似乎不仅要创建类型的实例 ,还要创建类本身。 Therefore, some of these classes are creating CLR types, and saving them off to real assemblies.
因此,其中一些类正在创建CLR类型,并将它们保存到实际程序集中。 That's pretty cool.
那很酷。
Edit Again 再次编辑
Some of the big dogs have said that there are more than the four types I listed above. 有些大狗说我上面列出的有四种以上的类型。 I used
Reflector ReSharper to find the derived types and found these (there could still be types missing): 我使用
Reflector ReSharper查找派生类型并找到它们(可能仍然缺少类型):
System.Type
System.RuntimeType
System.ReflectionOnlyType
System.Reflection.Emit.EnumBuilder
System.Reflection.Emit.GenericTypeParameterBuilder
System.Reflection.Emit.SymbolType
System.Reflection.Emit.TypeBuilder
System.Reflection.Emit.TypeBuilderInstantiation
System.Reflection.TypeDelegator
Edit Once More 再次编辑
as @MarcGravell stated, there's really no reason why you would want to derive a class from any of these. 正如@MarcGravell所说,你真的没有理由想从这些中派生出一个类。 You could, however, use them within a class of your own to encapsulate your own logic.
但是,您可以在自己的类中使用它们来封装自己的逻辑。
Yeah, don't inherit from that ;p There are things like RuntimeType
etc - Type
is the abstraction you should usually use. 是的,不要继承; p有
RuntimeType
等东西 - Type
是你应该经常使用的抽象。 If you want to represent a type dynamically at runtime, there are 2 main options; 如果要在运行时动态表示类型,则有两个主要选项;
dynamic
(via implementing IDynamicMetaObjectProvider
) dynamic
(通过实现IDynamicMetaObjectProvider
) TypeDescriptor
(via implementing ICustomTypeDescriptor
or providing a TypeDescriptionProvider
, and possibly a TypeConverter
) TypeDescriptor
(通过实现ICustomTypeDescriptor
或提供TypeDescriptionProvider
,可能还有TypeConverter
) If you want to do meta-programming (creating actual new types at runtime, rather than spoofing it), there is also TypeBuilder
如果你想进行元编程(在运行时创建实际的新类型,而不是欺骗它),还有
TypeBuilder
Type
is unsealed because it's actually designed to be inherrited from by the runtime. Type
是未密封的,因为它实际上被设计为由运行时继承。 There are many examples of this in the BCL 在BCL中有很多例子
The most interesting of these, IMHO, is RuntimeType
This is how a good majority of the Type
instances in a running system are implmented. 其中最有趣的是IMHO,它是
RuntimeType
这是运行系统中绝大多数Type
实例的实现方式。 Most calls to o.GetType()
will actually return a RuntimeType
instance. 大多数对
o.GetType()
调用实际上都会返回一个RuntimeType
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.