How type C # System.Type has a name property

I am sure this is due to the implementation of interfaces and inheritance.

In C #, how does the System.Type class have a property name? When I sample code of the Type class from metadata or using the Object Browser, I see that the Type class does not have:

string Name 

defined anywhere.

I also see that Type inherits from MemberInfo and implements _Type and IReflect (like this):

 public abstract class Type : MemberInfo, _Type, IReflect 

The base class MemberInfo has the property:

 public abstract string Name { get; } 

From what I know, this one has one that is redefined in the derived class due to the abstract modifier. I do not see it in type ...

Then in the _Type interface there is a property:

 string Name { get; } 

and since it is in the interface, it also does not have its own implementation.

Where is the name definition defined and how does it matter in the System.Type class? Or I don’t understand how inheritance and implementation of interfaces work for this class

+6
source share
2 answers

Note that System.Type itself is an abstract class. This means that it can be overridden in a subclass. In fact, you can see that the types at runtime are not really System.Type if you do something like this:

 typeof(Type).GetType().FullName; // System.RuntimeType 

System.RuntimeType is an internal type that you will not see in the documentation, but it overrides the Name property. It looks something like this:

 namespace System { internal class RuntimeType : Type, ISerializable, ICloneable { ... public override string Name { get { ... } } } } 

See this note in the official documentation:

A type is an abstract base class that allows many implementations. The system will always provide the derived class RuntimeType. With reflection, all classes starting with the word Runtime are created only once for each object in the system and support comparison operations.

+10
source

The abstract Name property of MemberInfo really inherited by System.Type .

The reason System.Type can implement the interface is because the abstract member of the instance is allowed to implement the interface if that member is public, of course, that Name . Members that implement interfaces can be inherited from the base classes.

The reason System.Type does not allow you to override the Name property is because System.Type itself is an abstract class. Thus, he can "leave" the abstract participant without implementation. Non-abstract types derived from it should still provide an implementation for this property (its get accessor).

Whenever you have a variable Type t = ...; and t.Name is t.Name on it, the execution type t must be a non-integrated class, and this class will have an implementation of Name .

+1
source

Source: https://habr.com/ru/post/951416/


All Articles