Typeクラスでは次のようなプロパティによって型の種類に関する情報を参照・取得することができます。
プロパティ | 意味 | |
---|---|---|
型の名称 | Name | 名前空間を含まない型名 (例: System.IO.Stream ならStream ) |
Namespace | 名前空間 (例: System.IO.Stream ならSystem.IO ) |
|
FullName | 名前空間を含む型名 (例: System.IO.Stream ならSystem.IO.Stream ) |
|
型の宣言箇所 | Assembly | 型が宣言されているアセンブリ |
Module | 型が宣言されているモジュール | |
DeclaringType | 入れ子になっている型の場合、その型を含んでいる型 | |
型の分類 | IsClass | 型がクラス型かどうか |
IsValueType | 型が値型(構造体・列挙体・プリミティブ数値型など)かどうか (関連:値型と参照型) | |
IsInterface | 型がインターフェイス型かどうか | |
IsEnum | 型が列挙体かどうか | |
IsArray | 型が配列型かどうか | |
IsPrimitive | 型がプリミティブ型かどうか (関連:型の種類・サイズ・精度・値域) | |
型の属性・状態・継承関係 | IsAbstract | 型が抽象型(abstract /MustInherit )かどうか |
IsSealed | 型がシールクラス(sealed /NotInheritable )かどうか |
|
IsPublic | 型がパブリックかどうか | |
IsNotPublic | 型が非パブリックかどうか | |
IsNested | 型が入れ子になっているか(他の型の内部で宣言されているか) | |
BaseType | 基底クラス(継承元)の型情報 (関連:インスタンスや型が一致するか・インターフェイスやクラスから派生しているか判定する §.基底クラスの取得) | |
ジェネリック型の分類 | IsGenericType | ジェネリック型かどうか (オープン型 List<> とクローズ型List<int> のどちらでもtrueとなる) |
IsGenericTypeDefinition | ジェネリック型定義(オープンジェネリック型)かどうか (オープン型 List<> ではtrue、クローズ型List<int> ではfalseとなる) |
|
IsConstructedGenericType
(.NET Framework 4.5以降) |
構築ジェネリック型(クローズジェネリック型)かどうか (オープン型 List<> ではfalse、クローズ型List<int> ではtrueとなる) |
|
ContainsGenericParameters | 具体的な型が指定されていない型パラメーターを含むかどうか ( List<> ではtrue、List<int> ではfalseとなる同様に Dictionary<,>.KeyCollection ではtrue、Dictionary<int,int>.KeyCollection ではfalseとなる) |
|
型パラメータの分類 | IsGenericParameter | 型がジェネリック型あるいはジェネリックメソッドの型パラメータを表すかどうか (§.型パラメータの分類) |
IsGenericTypeParameter
(.NET Standard 2.1/.NET Core 2.1以降) |
型がジェネリック型の型パラメータを表すかどうか (§.型パラメータの分類) | |
IsGenericMethodParameter
(.NET Standard 2.1/.NET Core 2.1以降) |
型がジェネリックメソッドの型パラメータを表すかどうか (§.型パラメータの分類) | |
プロパティ | 意味 |
次の例はTypeクラスのプロパティを参照して型の分類を行う例です。 この例で使用しているデリゲート型の判定に関しては後述の§.型がデリゲート型かどうか調べるを参照してください。
Typeクラスを使って型の分類を行う
using System;
using System.Collections.Generic;
class Sample {
static void Main()
{
ClassifyType(typeof(int));
ClassifyType(typeof(string));
ClassifyType(typeof(double[]));
ClassifyType(typeof(DateTime));
ClassifyType(typeof(KeyValuePair<,>));
ClassifyType(typeof(DayOfWeek));
ClassifyType(typeof(List<>));
ClassifyType(typeof(ICloneable));
ClassifyType(typeof(IEnumerable<>));
ClassifyType(typeof(EventHandler));
ClassifyType(typeof(Action<>));
}
static void ClassifyType(Type t)
{
Console.Write("{0}: ", t.FullName);
if (t.IsValueType) {
if (t.IsEnum) {
Console.WriteLine("列挙体");
}
else {
if (t.IsPrimitive) {
Console.WriteLine("値型(プリミティブ)");
}
else {
if (t.IsGenericType)
Console.Write("ジェネリック");
Console.WriteLine("構造体");
}
}
}
else {
if (t.IsArray) {
Console.WriteLine("配列型");
}
else {
if (t.IsGenericType)
Console.Write("ジェネリック");
if (t.IsInterface) {
Console.WriteLine("インターフェイス型");
}
else {
if (IsDelegate(t))
Console.WriteLine("デリゲート型");
else
Console.WriteLine("クラス型");
}
}
}
}
static bool IsDelegate(Type t)
{
return t.IsSubclassOf(typeof(Delegate)) || t.Equals(typeof(Delegate));
}
}
実行結果
System.Int32: 値型(プリミティブ) System.String: クラス型 System.Double[]: 配列型 System.DateTime: 構造体 System.Collections.Generic.KeyValuePair`2: ジェネリック構造体 System.DayOfWeek: 列挙体 System.Collections.Generic.List`1: ジェネリッククラス型 System.ICloneable: インターフェイス型 System.Collections.Generic.IEnumerable`1: ジェネリックインターフェイス型 System.EventHandler: デリゲート型 System.Action`1: ジェネリックデリゲート型
型の種類・分類については型の種類・サイズ・精度・値域や値型と参照型を合わせて参照してください。