Home   Cover Cover Cover Cover
 

Laufzeittypinformationen

A05.cs
using System;

class MyClass {
  int x, y;
}

class MySubClass: MyClass {
  int z;
}

class Test {
  
  static void PrintInfo(object obj) {
    Type t = obj.GetType();
    Console.WriteLine("type " + t.Name);
    Console.WriteLine("namespace: " + t.Namespace);
    Console.WriteLine("assembly:  " + t.Assembly.FullName);
    
    Console.Write("extends: ");
    Type bt = t;
    while (bt.BaseType != null) {
      bt = bt.BaseType;
      Console.Write(bt.Name + " ");
    }
    Console.WriteLine();
    
    Console.Write("implements: ");
    foreach (Type intf in t.GetInterfaces()) {
      Console.Write(intf.Name + " ");
    }
    Console.WriteLine();
    Console.WriteLine();
  }  
  
  public static void Main() {
    PrintInfo(3);
    PrintInfo("abc");
    PrintInfo(new MySubClass());
    PrintInfo(new int[3]);
  }
}

Dieses Programm liefert folgendes Ergebnis:

type Int32
namespace: System
assembly:  mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
extends: ValueType Object
implements: IComparable IFormattable IConvertible

type String
namespace: System
assembly:  mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
extends: Object
implements: IComparable ICloneable IConvertible IEnumerable

type MySubClass
namespace:
assembly:  Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
extends: MyClass Object
implements:

type Int32[]
namespace: System
assembly:  mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
extends: Array Object
implements: