Type.GetFields メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の Typeのフィールドを取得します。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetFields() |
現在の Typeのすべてのパブリック フィールドを返します。 |
| GetFields(BindingFlags) |
派生クラスでオーバーライドされた場合は、指定したバインド制約を使用して、現在の Typeに対して定義されているフィールドを検索します。 |
GetFields()
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
現在の Typeのすべてのパブリック フィールドを返します。
public:
cli::array <System::Reflection::FieldInfo ^> ^ GetFields();
public:
virtual cli::array <System::Reflection::FieldInfo ^> ^ GetFields();
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)]
public System.Reflection.FieldInfo[] GetFields();
public System.Reflection.FieldInfo[] GetFields();
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)>]
member this.GetFields : unit -> System.Reflection.FieldInfo[]
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;
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
注釈
.NET 6 以前のバージョンでは、GetFields メソッドは、アルファベット順や宣言順序などの特定の順序でフィールドを返しません。 コードは、フィールドが返される順序に依存しないようにする必要があります。これは、その順序が異なるためです。 ただし、.NET 7 以降では、順序付けはアセンブリ内のメタデータの順序に基づいて決定論的です。
次の表は、型に反映するときに、 Get メソッドによって返される基底クラスのメンバーを示しています。
| メンバーの種類 | Static | 非静的 |
|---|---|---|
| コンストラクター | いいえ | いいえ |
| フィールド | いいえ | Yes. フィールドは常に名前と署名によって非表示になります。 |
| イベント | 適用なし | 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。 |
| Method | いいえ | Yes. メソッド (仮想と非仮想の両方) は、名前で非表示にすることも、名前と署名で非表示にすることもできます。 |
| 入れ子にされた型 | いいえ | いいえ |
| 財産 | 適用なし | 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。 |
名前と署名による隠蔽は、カスタム修飾子、戻り値の型、パラメーター型、センチネル、そしてアンマネージド呼び出し規則を含む、署名のすべての部分を考慮します。 これはバイナリ比較です。
リフレクションの場合、プロパティとイベントは名前と署名によって非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしかない場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。
カスタム属性は、共通型システムの一部ではありません。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ FieldInfo オブジェクトを返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のパブリック フィールドを検索します。
こちらもご覧ください
適用対象
GetFields(BindingFlags)
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
- ソース:
- Type.cs
派生クラスでオーバーライドされた場合は、指定したバインド制約を使用して、現在の Typeに対して定義されているフィールドを検索します。
public:
abstract cli::array <System::Reflection::FieldInfo ^> ^ GetFields(System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)]
public abstract System.Reflection.FieldInfo[] GetFields(System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.FieldInfo[] GetFields(System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)>]
abstract member GetFields : System.Reflection.BindingFlags -> System.Reflection.FieldInfo[]
abstract member GetFields : System.Reflection.BindingFlags -> System.Reflection.FieldInfo[]
Public MustOverride Function GetFields (bindingAttr As BindingFlags) As FieldInfo()
パラメーター
返品
指定したバインド制約に一致する現在のFieldInfoに対して定義されているすべてのフィールドを表すType オブジェクトの配列。
-又は-
現在のFieldInfoに対してフィールドが定義されていない場合、または定義されたフィールドがバインド制約に一致しない場合は、Type型の空の配列。
実装
- 属性
例
次の例は、 GetFields(BindingFlags) メソッドの使用方法を示しています。
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引数に少なくとも 1 つのBindingFlags.InstanceとBindingFlags.Staticと、少なくとも 1 つのBindingFlags.NonPublicとBindingFlags.Publicが含まれている必要があります。
次の BindingFlags フィルター フラグを使用して、検索に含めるフィールドを定義できます。
インスタンス メソッドを含める
BindingFlags.Instanceを指定します。静的メソッドを含める
BindingFlags.Staticを指定します。検索にパブリック フィールドを含める
BindingFlags.Publicを指定します。BindingFlags.NonPublicを指定して、非パブリック フィールド (プライベート、内部、および保護されたフィールド) を検索に含めます。 基底クラスの保護されたフィールドと内部フィールドのみが返されます。基底クラスのプライベート フィールドは返されません。BindingFlags.FlattenHierarchyおよびpublic静的メンバーを階層に含めるprotectedを指定します。継承されたクラスprivate静的メンバーは含まれません。空
BindingFlags.DefaultPropertyInfo配列を返すには、単独で指定します。
次の BindingFlags 修飾子フラグを使用して、検索の動作を変更できます。
-
BindingFlags.DeclaredOnlyは、単に継承されたフィールドではなく、 Typeで宣言されているフィールドのみを検索します。
詳細については、System.Reflection.BindingFlags を参照してください。
.NET 6 以前のバージョンでは、GetFields メソッドは、アルファベット順や宣言順序などの特定の順序でフィールドを返しません。 コードは、フィールドが返される順序に依存しないようにする必要があります。これは、その順序が異なるためです。 ただし、.NET 7 以降では、順序付けはアセンブリ内のメタデータの順序に基づいて決定論的です。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ FieldInfo オブジェクトを返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のパブリック フィールドを検索します。