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 メソッド




英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Type.GetFields メソッド ()」の関連用語

Type.GetFields メソッド ()のお隣キーワード
検索ランキング

   

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



Type.GetFields メソッド ()のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS