简体   繁体   中英

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).

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. You can find the Type hierarchy on 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. 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):

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. 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. If you want to represent a type dynamically at runtime, there are 2 main options;

  • in 4.0, dynamic (via implementing IDynamicMetaObjectProvider )
  • before that, TypeDescriptor (via implementing ICustomTypeDescriptor or providing a TypeDescriptionProvider , and possibly a TypeConverter )

If you want to do meta-programming (creating actual new types at runtime, rather than spoofing it), there is also TypeBuilder

Type is unsealed because it's actually designed to be inherrited from by the runtime. There are many examples of this in the BCL

  • EnumBuilder
  • RuntimeType
  • TypeBuilder

The most interesting of these, IMHO, is RuntimeType This is how a good majority of the Type instances in a running system are implmented. Most calls to o.GetType() will actually return a RuntimeType instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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