C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#のType.GetEnumUnderlyingType()メソッドとは?列挙型の基になる型を取得する方法

C#のType.GetEnumUnderlyingType()メソッドは、現在の列挙型(enum)が内部で使用している基になる型(underlying type)を返します。列挙型の基になる型は、特に指定しない限り System.Int32 ですが、bytelong などを明示的に指定することもできます。このメソッドを使えば、その実際の型を実行時に取得できます。

構文

構文は以下のとおりです。

public virtual Type GetEnumUnderlyingType();

戻り値と例外

  • 戻り値: 現在の列挙型の基になる型を表す Type オブジェクト
  • 例外: 呼び出し元の型が列挙型ではない場合、ArgumentException がスローされる

例1: 列挙型に対して呼び出すケース

まずは、正しく列挙型に対して呼び出した場合の例を見てみましょう。列挙型 Vehicle を定義し、GetEnumNames() で定数名の一覧を取得した後、GetEnumUnderlyingType() の結果を出力しています。

using System;
public class Demo {
    enum Vehicle {Car, Bus, Bike, Airplane}
    public static void Main(){
        try {
            Vehicle v = Vehicle.Bike;
            Type type = v.GetType();
            string[] str = type.GetEnumNames();
            Console.WriteLine("GetEnumName() to return the constant name = " + str);
            Type type2 = type.GetEnumUnderlyingType();
            Console.Write("Enum Underlying type = "+type2);
            Console.WriteLine("\nListing constants ..");
            for (int i = 0; i < str.Length; i++)
                Console.Write("{0} ", str[i]);
        }
        catch (ArgumentException e){
            Console.WriteLine("Not an enum!");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

出力

上記のコードを実行すると、次のような結果になります。

GetEnumName() to return the constant name = System.String[]
Enum Underlying type = System.Int32
Listing constants ..
Car Bus Bike Airplane

この結果から、列挙型 Vehicle の基になる型が System.Int32 であることがわかります。また、GetEnumNames() によって定数名の一覧も正しく取得・表示されています。

例2: 列挙型以外に対して呼び出すケース

続いて、列挙型以外の型(ここでは typeof(int))に対して同じメソッドを呼び出した場合の例です。

using System;
public class Demo {
    enum Vehicle {Car, Bus, Bike, Airplane}
    public static void Main(){
        try {
            Type type = typeof(int);
            string[] str = type.GetEnumNames();
            Console.WriteLine("GetEnumName() to return the constant name = " + str);
            Type type2 = type.GetEnumUnderlyingType();
            Console.Write("Enum Underlying type = "+type2);
            Console.WriteLine("\nListing constants ..");
            for (int i = 0; i < str.Length; i++)
                Console.Write("{0} ", str[i]);
        }
        catch (ArgumentException e){
            Console.WriteLine("Not an enum!");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

出力

実行すると、次のように例外が発生します。

Not an enum!
System.ArgumentException

int 型は列挙型ではないため、GetEnumNames()GetEnumUnderlyingType() を呼び出した時点で ArgumentException がスローされ、catch ブロック内の処理が実行されていることが確認できます。

まとめ

  • Type.GetEnumUnderlyingType() は、列挙型の基になる型を Type オブジェクトとして返す
  • 基になる型を明示的に指定しない場合、既定値は System.Int32
  • 対象が列挙型でない場合は ArgumentException がスローされるため、事前に Type.IsEnum プロパティで判定しておくとより安全
  1. C#のType.GetArrayRank()メソッドの使い方を徹底解説

    C#におけるType.GetArrayRank()メソッドは、配列の次元数(ランク)を取得するためのメソッドです。多次元配列が何次元であるかをプログラム上で確認したい場合に便利な機能です。構文public virtual int GetArrayRank ();このメソッドは、対象の型が配列である場合にその次元数をint型で返します。戻り値の「ランク」とは、配列の角括弧内のカンマの数+1、つまり配列のインデックスの数を表します。使用例1:3次元配列のランクを取得するまずは、3次元のint型配列に対してType.GetArrayRank()メソッドを使用する基本的な例を見てみましょう。using

  2. C#のType.Equals()メソッドとは?使い方とサンプルコードを解説

    C#のType.Equals()メソッドは、現在のTypeインスタンスが表す基盤となるシステム型が、指定されたObjectまたはTypeの基盤となるシステム型と同一であるかどうかを判定します。リフレクション処理などで型同士を比較したい場合に役立つメソッドです。 構文 public virtual bool Equals (Type o); public override bool Equals (object o); パラメータ o:現在のTypeの基盤となるシステム型と比較する対象となるオブジェクトまたは型を指定します。 戻り値 両方の基盤システム型が同じ場合は true、異なる場合は fa