Type.GetFields メソッド

定義

現在の Type のフィールドを取得します。

オーバーロード

GetFields()

現在の Type のすべてのパブリック フィールドを返します。

GetFields(BindingFlags)

派生クラスによってオーバーライドされた場合、指定したバインディング制約を使用して、現在の Type に対して定義されているフィールドを検索します。

GetFields()

現在の Type のすべてのパブリック フィールドを返します。

public:
 cli::array <System::Reflection::FieldInfo ^> ^ GetFields();
public:
 virtual cli::array <System::Reflection::FieldInfo ^> ^ GetFields();
public System.Reflection.FieldInfo[] GetFields ();
member this.GetFields : unit -> System.Reflection.FieldInfo[]
abstract member GetFields : unit -> System.Reflection.FieldInfo[]
override this.GetFields : unit -> System.Reflection.FieldInfo[]
Public Function GetFields () As FieldInfo()

戻り値

現在の FieldInfo に対して定義されているすべてのパブリック フィールドを表す Type オブジェクトの配列。

または

現在の FieldInfo に対してパブリック フィールドが定義されていない場合は、Type 型の空の配列。

実装

次の例は、 メソッドの使用方法を GetFields() 示しています。

#using <system.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel::Design;

int main()
{
   try
   {
      // Get the type handle of a specified class.
      Type^ myType = ViewTechnology::typeid;

      // Get the fields of the specified class.
      array<FieldInfo^>^myField = myType->GetFields();
      Console::WriteLine( "\nDisplaying fields that have SpecialName attributes:\n" );
      for ( int i = 0; i < myField->Length; i++ )
      {
         // Determine whether or not each field is a special name.
         if ( myField[ i ]->IsSpecialName )
         {
            Console::WriteLine( "The field {0} has a SpecialName attribute.", myField[ i ]->Name );
         }
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0} ", e->Message );
   }
}
using System;
using System.Reflection;
using System.ComponentModel.Design;

class FieldInfo_IsSpecialName
{
    public static void Main()
    {
        try
        {
            // Get the type handle of a specified class.
            Type myType = typeof(ViewTechnology);

            // Get the fields of the specified class.
            FieldInfo[] myField = myType.GetFields();

            Console.WriteLine("\nDisplaying fields that have SpecialName attributes:\n");
            for(int i = 0; i < myField.Length; i++)
            {
                // Determine whether or not each field is a special name.
                if(myField[i].IsSpecialName)
                {
                    Console.WriteLine("The field {0} has a SpecialName attribute.",
                        myField[i].Name);
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception : {0} " , e.Message);
        }
    }
}
open System.ComponentModel.Design

try
    // Get the type handle of a specified class.
    let myType = typeof<ViewTechnology>

    // Get the fields of the specified class.
    let myFields = myType.GetFields()

    printfn $"\nDisplaying fields that have SpecialName attributes:\n"
    for field in myFields do
        // Determine whether or not each field is a special name.
        if field.IsSpecialName then
            printfn $"The field {field.Name} has a SpecialName attribute."
with e ->
    printfn $"Exception : {e.Message} "
Imports System.Reflection
Imports System.ComponentModel.Design

Class FieldInfo_IsSpecialName

    Public Shared Sub Main()
        Try
            ' Get the type handle of a specified class.
            Dim myType As Type = GetType(ViewTechnology)

            ' Get the fields of a specified class.
            Dim myField As FieldInfo() = myType.GetFields()

            Console.WriteLine(ControlChars.Cr + "Displaying fields that have SpecialName attributes:" + ControlChars.Cr)
            Dim i As Integer
            For i = 0 To myField.Length - 1
                ' Determine whether or not each field is a special name.
                If myField(i).IsSpecialName Then
                    Console.WriteLine("The field {0} has a SpecialName attribute.", myField(i).Name)
                End If
            Next i
        Catch e As Exception
            Console.WriteLine("Exception : {0} ", e.Message.ToString())
        End Try
    End Sub
End Class

注釈

メソッドは GetFields 、アルファベット順や宣言順序など、特定の順序のフィールドを返しません。 フィールドが返される順序に依存しないコードは、その順序が異なるためです。

次の表は、型に反映するときに メソッドによって Get 返される基底クラスのメンバーを示しています。

メンバーの型 スタティック 非静的
コンストラクター いいえ いいえ
フィールド いいえ はい。 フィールドは常に名前と署名で非表示になります。
event 適用なし 一般的な型システムルールは、継承が プロパティを実装するメソッドの継承と同じであるという点です。 リフレクションでは、プロパティは名前と署名による非表示として扱います。 以下の注 2 を参照してください。
メソッド いいえ はい。 メソッド (仮想と非仮想の両方) は、名前で非表示にすることも、名前と署名で非表示にすることもできます。
入れ子になった型 いいえ いいえ
プロパティ 適用なし 一般的な型システムルールは、継承が プロパティを実装するメソッドの継承と同じであるという点です。 リフレクションでは、プロパティは名前と署名による非表示として扱います。 以下の注 2 を参照してください。
  1. 名前と署名による非表示は、カスタム修飾子、戻り値の型、パラメーター型、sentinel、アンマネージド呼び出し規則など、署名のすべての部分を考慮します。 これはバイナリ比較です。

  2. リフレクションの場合、プロパティとイベントは名前と署名によって非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしかない場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。

  3. カスタム属性は、共通型システムの一部ではありません。

現在 Type の が構築されたジェネリック型を表している場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つオブジェクトを返 FieldInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のパブリック フィールドを検索します。

こちらもご覧ください

適用対象

GetFields(BindingFlags)

派生クラスによってオーバーライドされた場合、指定したバインディング制約を使用して、現在の Type に対して定義されているフィールドを検索します。

public:
 abstract cli::array <System::Reflection::FieldInfo ^> ^ GetFields(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.FieldInfo[] GetFields (System.Reflection.BindingFlags bindingAttr);
abstract member GetFields : System.Reflection.BindingFlags -> System.Reflection.FieldInfo[]
Public MustOverride Function GetFields (bindingAttr As BindingFlags) As FieldInfo()

パラメーター

bindingAttr
BindingFlags

検索を実施する方法を指定する列挙値のビットごとの組み合わせ。

または

空の配列を返す場合は Default

戻り値

現在の Type に対して定義されているフィールドのうち、指定したバインディング制約に一致するすべてのフィールドを表す FieldInfo オブジェクトの配列。

または

現在の FieldInfo に対してフィールドが定義されていないか、または定義されているフィールドの中にバインディング制約に一致するものが存在しない場合は、Type 型の空の配列。

実装

次の例は、 メソッドの使用方法を GetFields(BindingFlags) 示しています。

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
public ref class AttributesSample
{
public:
   void Mymethod( int int1m, [Out]interior_ptr<String^> str2m, interior_ptr<String^> str3m )
   {
       *str2m = "in Mymethod";
   }
};

void PrintAttributes( Type^ attribType, int iAttribValue )
{
   if (  !attribType->IsEnum )
   {
      Console::WriteLine( "This type is not an enum." );
      return;
   }

   array<FieldInfo^>^fields = attribType->GetFields( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static) );
   for ( int i = 0; i < fields->Length; i++ )
   {
      int fieldvalue = safe_cast<Int32>(fields[ i ]->GetValue( nullptr ));
      if ( (fieldvalue & iAttribValue) == fieldvalue )
      {
         Console::WriteLine( fields[ i ]->Name );
      }
   }
}

int main()
{
   Console::WriteLine( "Reflection.MethodBase.Attributes Sample" );

   // Get the type.
   Type^ MyType = Type::GetType( "AttributesSample" );

   // Get the method Mymethod on the type.
   MethodBase^ Mymethodbase = MyType->GetMethod( "Mymethod" );

   // Display the method name.
   Console::WriteLine( "Mymethodbase = {0}", Mymethodbase );

   // Get the MethodAttribute enumerated value.
   MethodAttributes Myattributes = Mymethodbase->Attributes;

   // Display the flags that are set.
   PrintAttributes( System::Reflection::MethodAttributes::typeid, (int)Myattributes );
   return 0;
}

using System;
using System.Reflection;

class AttributesSample
{
    public void Mymethod (int int1m, out string str2m, ref string str3m)
    {
        str2m = "in Mymethod";
    }

    public static int Main(string[] args)
    {
        Console.WriteLine ("Reflection.MethodBase.Attributes Sample");

        // Get the type.
        Type MyType = Type.GetType("AttributesSample");

        // Get the method Mymethod on the type.
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");

        // Display the method name.
        Console.WriteLine("Mymethodbase = " + Mymethodbase);

        // Get the MethodAttribute enumerated value.
        MethodAttributes Myattributes = Mymethodbase.Attributes;

        // Display the flags that are set.
        PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
        return 0;
    }

    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
        if (!attribType.IsEnum)
        {
            Console.WriteLine("This type is not an enum.");
            return;
        }

        FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
        for (int i = 0; i < fields.Length; i++)
        {
            int fieldvalue = (int)fields[i].GetValue(null);
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}
open System
open System.Reflection

type AttributesSample() =
    member _.Mymethod(int1m: int, str2m: string outref, str3m: string byref) =
        str2m <- "in Mymethod"

let printAttributes (attribType: Type) iAttribValue =
    if not attribType.IsEnum then
        printfn "This type is not an enum."
    else
        let fields = attribType.GetFields(BindingFlags.Public ||| BindingFlags.Static)
        for f in fields do
            let fieldvalue = f.GetValue null :?> int
            if fieldvalue &&& iAttribValue = fieldvalue then
                printfn $"{f.Name}"

printfn "Reflection.MethodBase.Attributes Sample"

// Get the type.
let MyType = Type.GetType "AttributesSample"

// Get the method Mymethod on the type.
let Mymethodbase = MyType.GetMethod "Mymethod"

// Display the method name.
printfn $"Mymethodbase = {Mymethodbase}"

// Get the MethodAttribute enumerated value.
let Myattributes = Mymethodbase.Attributes

// Display the flags that are set.
printAttributes typeof<MethodAttributes> (int Myattributes)
Imports System.Reflection

Class AttributesSample

    Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String)
        str2m = "in Mymethod"
    End Sub

    Public Shared Function Main(ByVal args() As String) As Integer
        Console.WriteLine("Reflection.MethodBase.Attributes Sample")

        ' Get the type.
        Dim MyType As Type = Type.GetType("AttributesSample")

        ' Get the method Mymethod on the type.
        Dim Mymethodbase As MethodBase = MyType.GetMethod("Mymethod")

        ' Display the method name.
        Console.WriteLine("Mymethodbase = {0}.", Mymethodbase)

        ' Get the MethodAttribute enumerated value.
        Dim Myattributes As MethodAttributes = Mymethodbase.Attributes

        ' Display the flags that are set.
        PrintAttributes(GetType(System.Reflection.MethodAttributes), CInt(Myattributes))
        Return 0
    End Function 'Main

    Public Shared Sub PrintAttributes(ByVal attribType As Type, ByVal iAttribValue As Integer)
        If Not attribType.IsEnum Then
            Console.WriteLine("This type is not an enum.")
            Return
        End If
        Dim fields As FieldInfo() = attribType.GetFields((BindingFlags.Public Or BindingFlags.Static))
        Dim i As Integer
        For i = 0 To fields.Length - 1
            Dim fieldvalue As Integer = CType(fields(i).GetValue(Nothing), Int32)
            If (fieldvalue And iAttribValue) = fieldvalue Then
                Console.WriteLine(fields(i).Name)
            End If
        Next i
    End Sub
End Class

注釈

オーバーロードがGetFields(BindingFlags)プロパティ情報を正常に取得するには、 引数にbindingAttr、 と BindingFlags.Staticの少なくとも 1 つとBindingFlags.Instance、 と の少なくとも 1 つをBindingFlags.PublicBindingFlags.NonPublic含める必要があります。

BindingFlags のフィルター フラグを使用して、検索に含めるフィールドを定義できます。

  • インスタンス メソッドを含めるには、 を指定 BindingFlags.Instance します。

  • 静的メソッドを含めるには、 を指定 BindingFlags.Static します。

  • 検索にパブリック フィールドを含めるには、 を指定 BindingFlags.Public します。

  • 非パブリック フィールド (つまり、プライベート、内部、および保護されたフィールド) を検索に含めるには、 を指定 BindingFlags.NonPublic します。 基底クラスの保護されたフィールドと内部フィールドのみが返されます。基底クラスのプライベート フィールドは返されません。

  • を指定BindingFlags.FlattenHierarchyして、階層privateのメンバーとprotected静的メンバーを含publicめます。継承されたクラスの静的メンバーは含まれません。

  • PropertyInfoの配列を返すには、単独で を指定BindingFlags.Defaultします。

検索のしくみを変更するには、次 BindingFlags の修飾子フラグを使用できます。

  • BindingFlags.DeclaredOnly で宣言されている Typeフィールドのみを検索する場合は 。単に継承されたフィールドは検索されません。

詳細については、「System.Reflection.BindingFlags」を参照してください。

メソッドは GetFields 、アルファベット順や宣言順序など、特定の順序のフィールドを返しません。 フィールドが返される順序に依存しないコードは、その順序が異なるためです。

現在 Type の が構築されたジェネリック型を表している場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つオブジェクトを返 FieldInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のパブリック フィールドを検索します。

こちらもご覧ください

適用対象