Base Types and Interfaces

Type exposes a BaseType property:


using System;
using System.Reflection;
using System.Collections.Generic;

class MainClass
{
    static void Main()
    {
        Type base1 = typeof(System.String).BaseType;
        Type base2 = typeof(System.IO.FileStream).BaseType;

        Console.WriteLine(base1.Name);  // Object
        Console.WriteLine(base2.Name);  // Stream

    }

}

The output:


Object
Stream

The GetInterfaces method returns the interfaces that a type implements:


using System;
using System.Reflection;
using System.Collections.Generic;

class MainClass
{
    static void Main()
    {
        foreach (Type iType in typeof(Guid).GetInterfaces())
            Console.WriteLine(iType.Name);

    }

}

The output:


IFormattable
IComparable
IComparable`1
IEquatable`1

The following code does the static and dynamic interface type checking.


using System;
using System.Reflection;
using System.Collections.Generic;

class MainClass
{
    static void Main()
    {
        Type target = typeof(IFormattable);

        bool isTrue = target is IFormattable; // Static C# operator 
        bool alsoTrue = target.IsInstanceOfType(target);  // Dynamic equivalent
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.