Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
}
like image 687
Aaron Anodide Avatar asked Feb 20 '12 19:02

Aaron Anodide


People also ask

What is a non sealed class?

According to this documentation, the non-sealed class allows opening part of the inheritance hierarchy to the world. It means the root sealed class permits only a closed set of subclasses to extend it.

Why do we need sealed class?

Sealed class is used to stop a class to be inherited. You cannot derive or extend any class from it. Sealed method is implemented so that no other class can overthrow it and implement its own method.

How do you seal a class in Java?

To seal a class, add the sealed modifier to its declaration. Then, after any extends and implements clauses, add the permits clause. This clause specifies the classes that may extend the sealed class.

How do you make a class sealed in C++?

In C++11, you can seal a class by using final keyword in the definition as: class A final //note final keyword is used after the class name { //... }; class B : public A //error - because class A is marked final (sealed). { // so A cannot be derived from. //... };


1 Answers

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.

like image 177
cwharris Avatar answered Oct 12 '22 23:10

cwharris