Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my interface's BaseType null?

Interface definition

public interface IPayeePayrollRunInitialPayElementData : IPayeePayrollRunPayElementData

But in my code the BaseType of my interface is null. I cannot make any sense of this!

Breakpoint and watches

like image 625
Peter Morris Avatar asked May 17 '13 10:05

Peter Morris


People also ask

How do you declare an interface in C#?

To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the members in the interface are declared with the empty body and are public and abstract by default. A class that implements interface must implement all the methods declared in the interface.

How do I find the interface type?

GetInterface(String) Method This method is used to search for the interface with the specified name. Syntax: public Type GetInterface (string name); Here, it takes the string containing the name of the interface to get. For generic interfaces, this is the mangled name.

What is the base type?

The base type is the type from which the current type directly inherits. Object is the only type that does not have a base type, therefore null is returned as the base type of Object.

What is interface C?

What Does Interface Mean? Interface, in C#, is a code structure that defines a contract between an object and its user. It contains a collection of semantically similar properties and methods that can be implemented by a class or a struct that adheres to the contract.


2 Answers

Because it's defined to be so?

Interfaces inherit from zero or more base interfaces; therefore, this property returns null if the Type object represents an interface. The base interfaces can be determined with GetInterfaces or FindInterfaces.

like image 62
Damien_The_Unbeliever Avatar answered Sep 30 '22 17:09

Damien_The_Unbeliever


From Type.BaseType page;

Interfaces inherit from zero or more base interfaces; therefore, this property returns null if the Type object represents an interface. The base interfaces can be determined with GetInterfaces or FindInterfaces.

public interface IPayeePayrollRunInitialPayElementData : IPayeePayrollRunPayElementData
{ }

public interface IPayeePayrollRunPayElementData
{ }

class Program
{
    static void Main(string[] args)
    {
        foreach (Type tinterface in typeof(IPayeePayrollRunInitialPayElementData).GetInterfaces())
        {
            Console.WriteLine(tinterface.FullName);
        }
    }
}

Output will be;

IPayeePayrollRunPayElementData

Here is a DEMO.

like image 21
Soner Gönül Avatar answered Sep 30 '22 18:09

Soner Gönül