Type.GetFields メソッド ()
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As Type Dim returnValue As FieldInfo() returnValue = instance.GetFields
現在の Type に対して定義されているすべてのパブリック フィールドを表す FieldInfo オブジェクトの配列。 または 現在の Type に対してパブリック フィールドが定義されていない場合は、FieldInfo 型の空の配列。

GetFields メソッドから返されるフィールドは、アルファベット順や宣言順などの特定の順序で返されるわけではありません。したがって、フィールドが返される順序に依存するようなコードは避ける必要があります。
型に対するリフレクション時に Get メソッドによって返される基本クラスのメンバを次の表に示します。
メンバ型 | 非静的 | |
---|---|---|
いいえ | いいえ | |
いいえ | ||
適用なし | 共通型システムの規則では、継承は、プロパティを実装するメソッドの継承と同じになります。リフレクションは、プロパティを名前と署名によって隠ぺいされているとして扱います。下記のメモ 2 を参照してください。 | |
いいえ | はい。メソッド (仮想メソッドと非仮想メソッドの両方) は、名前によって隠蔽することもできますし、名前と署名によって隠蔽することもできます。 | |
いいえ | いいえ | |
適用なし | 共通型システムの規則では、継承は、プロパティを実装するメソッドの継承と同じになります。リフレクションは、プロパティを名前と署名によって隠ぺいされているとして扱います。下記のメモ 2 を参照してください。 |
-
名前と署名による隠ぺいでは、カスタム修飾子、戻り値の型、パラメータの型、sentinel、およびアンマネージ呼び出し規約を含めて、署名のすべての部分が判断の対象となります。これはバイナリ比較です。
-
リフレクションの場合、プロパティおよびイベントは名前と署名によって隠ぺいされています。基本クラスに get アクセサと set アクセサの両方を持つプロパティがあり、派生クラスには get アクセサしかない場合、派生クラスのプロパティにより基本クラスのプロパティが隠ぺいされ、基本クラスの set アクセサにはアクセスできません。
現在の 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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Type.GetFields メソッド (BindingFlags)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As Type Dim bindingAttr As BindingFlags Dim returnValue As FieldInfo() returnValue = instance.GetFields(bindingAttr)
- bindingAttr
検索の実行方法を指定する 1 つ以上の BindingFlags から成るビット マスク。
または
null 参照 (Visual Basic では Nothing) を返す 0。
現在の Type に対して定義されているフィールドのうち、指定したバインディング制約に一致するすべてのフィールドを表す FieldInfo オブジェクトの配列。 または 現在の Type に対してフィールドが定義されていないか、または定義されているフィールドの中にバインディング制約に一致するものが存在しない場合は、FieldInfo 型の空の配列。

GetFields メソッドから返されるフィールドは、アルファベット順や宣言順などの特定の順序で返されるわけではありません。したがって、フィールドが返される順序に依存するようなコードは避ける必要があります。
次の BindingFlags フィルタ フラグは、検索対象に含めるフィールドを定義するために使用できます。
-
戻り値を取得するには、BindingFlags.Instance または BindingFlags.Static のいずれかを指定する必要があります。
-
検索対象にパブリックでないフィールド (つまり、プライベート フィールドやプロテクト フィールド) を含めるための BindingFlags.NonPublic を指定します。
-
階層構造の上位にある public 静的メンバおよび protected 静的メンバを検索対象に含めるには、BindingFlags.FlattenHierarchy を指定します。継承クラスの private 静的メンバは含まれません。
次の 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 */


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Type.GetFields メソッド

名前 | 説明 |
---|---|
Type.GetFields () | 現在の Type のすべてのパブリック フィールドを返します。 .NET Compact Framework によってサポートされています。 |
Type.GetFields (BindingFlags) | 派生クラスによってオーバーライドされた場合、指定したバインディング制約を使用して、現在の Type に対して定義されているフィールドを検索します。 .NET Compact Framework によってサポートされています。 |

_Type.GetFields メソッド
Weblioに収録されているすべての辞書からType.GetFieldsを検索する場合は、下記のリンクをクリックしてください。

- Type.GetFieldsのページへのリンク