Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does c# System.Type Type have a name property

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

like image 599
Josh R Avatar asked Aug 09 '13 19:08

Josh R


People also ask

How does C language work?

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.

What is the use of in C?

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.


2 Answers

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.

like image 97
p.s.w.g Avatar answered Oct 26 '22 12:10

p.s.w.g


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.

like image 35
Jeppe Stig Nielsen Avatar answered Oct 26 '22 10:10

Jeppe Stig Nielsen