I'm pretty sure this is related to implementing interfaces and inheritance.
In c# how does the System.Type class have the property Name? When I example the code of the Type class from metadata or using the Object Browser I see the Type class doesn't 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 has to be overridden in the derived class because of the abstract modifier. I don't see it in Type...
Then in the _Type interface there is a property:
string Name { get; }
and because that is in an interface it has no implementation of its own either.
Where is Name getting defined and how do it have a value in the System.Type class? Or am I not understanding how inheritance and implementing interfaces works for this class
C is what's referred to as a compiled language, meaning you have to use a compiler to turn the code into an executable file before you can run it. The code is written into one or more text files, which you can open, read and edit in any text editor, such as Notepad in Windows, TextEdit on a Mac, and gedit in Linux.
In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.
Notice that System.Type
is itself an abstract class. That means it can be overriden in a subclass. In fact, you can see that types at run-time are not actually System.Type
's if you do something like this:
typeof(Type).GetType().FullName; // System.RuntimeType
The System.RuntimeType
is an internal type that you won't see in the documentation, but it does override the Name
property. It looks a little like this:
namespace System
{
internal class RuntimeType : Type, ISerializable, ICloneable
{
...
public override string Name
{
get { ... }
}
}
}
See this note in the official documentation:
Type is an abstract base class that allows multiple implementations. The system will always provide the derived class RuntimeType. In reflection, all classes beginning with the word Runtime are created only once per object in the system and support comparison operations.
The abstract property Name
from MemberInfo
is indeed inherited by System.Type
.
The reason why System.Type
can implement the interface, is that an abstract instance member is allowed to implement an interface, as long as that member is public, of course, which Name
is. Members that implement interfaces can be inherited from base classes.
The reason why System.Type
is allowed to not override the Name
property, is that System.Type
is itself an abstract class. So it can "leave" the abstract member with no implementation. Non-abstract types deriving from it must still give an implementation for this property (its get
accessor).
Whenever you have a variable Type t = ...;
and call t.Name
on it, the run-time type of t
will have to be some non-abstract class, and that class will have an implementation of Name
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With