Type.GetFieldsとは? わかりやすく解説

Type.GetFields メソッド ()

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

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function GetFields As
 FieldInfo()
Dim instance As Type
Dim returnValue As FieldInfo()

returnValue = instance.GetFields
public FieldInfo[] GetFields ()
public:
virtual array<FieldInfo^>^ GetFields () sealed
public final FieldInfo[] GetFields ()
public final function GetFields () : FieldInfo[]

戻り値
現在の Type に対して定義されているすべてのパブリック フィールドを表す FieldInfo オブジェクトの配列。 または 現在の Type に対してパブリック フィールドが定義されていない場合は、FieldInfo 型の空の配列。

解説解説

GetFields メソッドから返されるフィールドは、アルファベット順や宣言順などの特定の順序で返されるわけではありません。したがって、フィールドが返される順序に依存するようなコードは避ける必要があります。

型に対するリフレクション時に Get メソッドによって返される基本クラスのメンバを次の表に示します。

  1. 名前と署名による隠ぺいでは、カスタム修飾子、戻り値の型、パラメータの型、sentinel、およびアンマネージ呼び出し規約を含めて、署名のすべての部分が判断の対象となります。これはバイナリ比較です。

  2. リフレクションの場合、プロパティおよびイベントは名前と署名によって隠ぺいされています。基本クラスに get アクセサと set アクセサの両方を持つプロパティがあり、派生クラスには get アクセサしかない場合、派生クラスのプロパティにより基本クラスのプロパティが隠ぺいされ、基本クラスの set アクセサにはアクセスできません。

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

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

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

使用例使用例

GetFields() メソッドを使用する例を次に示します。

Imports System
Imports System.Reflection
Imports System.ComponentModel.Design
Imports Microsoft.VisualBasic

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 'Main
End Class 'FieldInfo_IsSpecialName
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);
        }
    }
}
#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 );
   }
}
import System.*;
import System.Reflection.*;
import System.ComponentModel.Design.*;

class FieldInfoIsSpecialName
{
    public static void main(String[]
 args)
    {
        try {
            // Get the type handle of a specified class.
            Type myType = ViewTechnology.class.ToType();

            // 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].get_IsSpecialName()) {
                    Console.WriteLine("The field {0} has a SpecialName"
 
                        + " attribute.", myField[i].get_Name());
                }
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : {0} ", e.get_Message());
        }
    } //main
} //FieldInfoIsSpecialName
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Type.GetFields メソッド (BindingFlags)

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

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public MustOverride Function
 GetFields ( _
    bindingAttr As BindingFlags _
) As FieldInfo()
Dim instance As Type
Dim bindingAttr As BindingFlags
Dim returnValue As FieldInfo()

returnValue = instance.GetFields(bindingAttr)
public abstract FieldInfo[] GetFields (
    BindingFlags bindingAttr
)
public:
virtual array<FieldInfo^>^ GetFields (
    BindingFlags bindingAttr
) abstract
public abstract FieldInfo[] GetFields (
    BindingFlags bindingAttr
)
public abstract function GetFields (
    bindingAttr : BindingFlags
) : FieldInfo[]

パラメータ

bindingAttr

検索の実行方法を指定する 1 つ以上の BindingFlags から成るビット マスク。

または

null 参照 (Visual Basic では Nothing) を返す 0。

戻り値
現在の Type に対して定義されているフィールドのうち、指定したバインディング制約に一致するすべてのフィールドを表す FieldInfo オブジェクトの配列。 または 現在の Type に対してフィールドが定義されていないか、または定義されているフィールドの中にバインディング制約に一致するものが存在しない場合は、FieldInfo 型の空の配列。

解説解説

GetFields メソッドから返されるフィールドは、アルファベット順や宣言順などの特定の順序で返されるわけではありません。したがって、フィールドが返される順序に依存するようなコードは避ける必要があります。

次の BindingFlags フィルタ フラグは、検索対象に含めるフィールドを定義するために使用できます。

次の BindingFlags 修飾フラグは、検索方法を変更するために使用できます。

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

要求された型がパブリックではなく、呼び出し元に現在のアセンブリ外の非パブリック オブジェクトをリフレクションするための ReflectionPermission がない場合、このメソッドは null 参照 (Visual Basic では Nothing) を返します。

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

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

使用例使用例

GetFields(BindingFlags) メソッドを使用する例を次に示します。

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Class AttributesSample

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

    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 'PrintAttributes
End Class 'AttributesSample
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 = (Int32)fields[i].GetValue(null);
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}
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;
}
import System.*;
import System.Reflection.*;

class AttributesSample
{   
    public void MyMethod(int
 int1m,
        /** @ref
         */ String str2m,
        /** @ref
         */ String str3m)
    {
        str2m = "in MyMethod";
    } //MyMethod

    public static void 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.get_Attributes();

        // Display the flags that are set.
        PrintAttributes(System.Reflection.MethodAttributes .class.ToType()
,
            (int)(myAttributes));
    } //main

    public static void PrintAttributes(Type
 attribType, int iAttribValue)
    {
        if (!(attribType.get_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) ((Int32)(fields[i].GetValue(null)));
            if ((fieldValue & iAttribValue) == fieldValue
  ) {
                Console.WriteLine(fields[i].get_Name());
            }
        } 
    } //PrintAttributes
} //AttributesSample
 import System;
 import System.Reflection;
 
 class AttributesSample
 {
    public function Mymethod (int1m : int)
 : void
    {
    }
 
    public static function
 Main() : void
    {      
       Console.WriteLine ("Reflection.MethodBase.Attributes Sample");
       
       // Get our type
       var MyType : Type = Type.GetType("AttributesSample");
 
       // Get the method Mymethod on our type
       var Mymethodbase : MethodBase = MyType.GetMethod("Mymethod");
 
       // Print out the method
       Console.WriteLine("Mymethodbase = " + Mymethodbase);
 
       // Get the MethodAttribute enumerated value
       var Myattributes : MethodAttributes = Mymethodbase.Attributes;
 
       // print out the flags set
       PrintAttributes( System.Reflection.MethodAttributes, int(Myattributes)
 );
    }
 
 
    public static function
 PrintAttributes( attribType : Type, iAttribValue : int ) : void
 
    {
       if ( ! attribType.IsEnum ) { Console.WriteLine( "This
 type is not an enum" ); return; }
 
       var fields : FieldInfo[] = attribType.GetFields(BindingFlags.Public
 | BindingFlags.Static);
       for ( var i:int =
 0; i < fields.Length; i++ )
       {
          var fieldvalue : int = int(fields[i].GetValue(null));
          if ( (fieldvalue & iAttribValue) == fieldvalue )
          {
             Console.WriteLine( "\t" + fields[i].Name );
          }
       }
    }
 }
 AttributesSample.Main();
/* 
 This code produces the following output:
 
Reflection.MethodBase.Attributes Sample
Mymethodbase = Void Mymethod(Int32)
        PrivateScope
        FamANDAssem
        Family
        Public
        Virtual
        HideBySig
        VtableLayoutMask
        ReuseSlot
        NewSlot
*/
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
FieldInfo
BindingFlags
DefaultBinder
ReflectionPermission
GetField

Type.GetFields メソッド


_Type.GetFields メソッド




英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Type.GetFields」の関連用語

Type.GetFieldsのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Type.GetFieldsのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS