Type.GetFields 메서드

정의

현재 Type의 필드를 가져옵니다.

오버로드

GetFields()

현재 Type의 모든 public 필드를 반환합니다.

GetFields(BindingFlags)

파생 클래스에서 재정의되면, 현재 Type에 대해 정의된 필드를 지정된 바인딩 제약 조건으로 검색합니다.

GetFields()

현재 Type의 모든 public 필드를 반환합니다.

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[]

현재 FieldInfo에 대해 정의된 모든 public 필드를 나타내는 Type 개체의 배열입니다.

또는 현재 FieldInfo에 대해 정의된 public 필드가 없는 경우 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);
        }
    }
}
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 있습니다.

멤버 형식 정적 비정적
생성자 아니요 아니요
필드 아니요 예. 필드는 항상 이름 및 서명을 숨깁니다.
이벤트 해당 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 hide-by-name-and-signature로 처리합니다. 아래 참고 2를 참조하세요.
메서드 아니요 예. 메서드(가상 및 가상이 아닌 메서드)는 이름별로 숨기거나 이름 및 서명으로 숨길 수 있습니다.
중첩 형식 아니요 아니요
속성 해당 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 hide-by-name-and-signature로 처리합니다. 아래 참고 2를 참조하세요.
  1. 이름별 숨기기 및 서명은 사용자 지정 한정자, 반환 형식, 매개 변수 형식, sentinels 및 관리되지 않는 호출 규칙을 포함하여 서명의 모든 부분을 고려합니다. 이진 비교입니다.

  2. 리플렉션의 경우 속성 및 이벤트는 이름별 및 서명으로 숨깁니다. 기본 클래스에 get 및 set 접근자를 모두 가진 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기며 기본 클래스의 setter에 액세스할 수 없습니다.

  3. 사용자 지정 특성은 공용 형식 시스템의 일부가 아닙니다.

현재 가 Type 구성된 제네릭 형식을 나타내는 경우 이 메서드는 FieldInfo 형식 매개 변수가 적절한 형식 인수로 대체된 개체를 반환합니다.

현재 가 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 public 필드를 검색합니다.

추가 정보

적용 대상

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입니다.

반환

FieldInfo[]

현재 FieldInfo에 대해 정의된 필드 중 지정된 바인딩 제약 조건과 일치하는 모든 필드를 나타내는 Type 개체의 배열입니다.

또는 현재 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);
            }
        }
    }
}
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.Instance BindingFlags.Static BindingFlags.NonPublic BindingFlags.Public 합니다.

다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 필드를 정의할 수 있습니다.

  • BindingFlags.Instance인스턴스 메서드를 포함하도록 를 지정합니다.

  • BindingFlags.Static정적 메서드를 포함하도록 를 지정합니다.

  • BindingFlags.Public검색에 공용 필드를 포함하도록 지정합니다.

  • BindingFlags.NonPublicpublic이 아닌 필드(즉, private, internal 및 protected 필드)를 검색에 포함하도록 지정합니다. 기본 클래스의 보호된 필드와 내부 필드만 반환됩니다. 기본 클래스의 private 필드는 반환되지 않습니다.

  • BindingFlags.FlattenHierarchy public 계층에 및 정적 멤버를 포함하도록 protected 지정합니다. private 상속된 클래스의 정적 멤버는 포함되지 않습니다.

  • BindingFlags.Default빈 배열을 반환하려면 단독으로 를 지정합니다. PropertyInfo

다음 BindingFlags 한정자 플래그를 사용하여 검색 작동 방식을 변경할 수 있습니다.

  • BindingFlags.DeclaredOnly 에 선언된 필드만 검색하려면 Type 이고, 단순히 상속된 필드는 검색하지 않습니다.

자세한 내용은 System.Reflection.BindingFlags를 참조하세요.

GetFields메서드는 사전순 또는 선언 순서와 같은 특정 순서로 필드를 반환하지 않습니다. 해당 순서가 달라지므로 코드는 필드가 반환되는 순서에 따라 달라지지 않아야 합니다.

현재 가 Type 구성된 제네릭 형식을 나타내는 경우 이 메서드는 FieldInfo 형식 매개 변수가 적절한 형식 인수로 대체된 개체를 반환합니다.

현재 가 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 public 필드를 검색합니다.

추가 정보

적용 대상