Type.GetField 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 Type.의 특정 필드를 가져옵니다.
오버로드
| Name | Description |
|---|---|
| GetField(String) |
지정된 이름의 공용 필드를 검색합니다. |
| GetField(String, BindingFlags) |
지정된 바인딩 제약 조건을 사용하여 지정된 필드를 검색합니다. |
GetField(String)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 이름의 공용 필드를 검색합니다.
public:
System::Reflection::FieldInfo ^ GetField(System::String ^ name);
public:
virtual System::Reflection::FieldInfo ^ GetField(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)]
public System.Reflection.FieldInfo? GetField(string name);
public System.Reflection.FieldInfo GetField(string name);
public System.Reflection.FieldInfo? GetField(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)>]
member this.GetField : string -> System.Reflection.FieldInfo
member this.GetField : string -> System.Reflection.FieldInfo
abstract member GetField : string -> System.Reflection.FieldInfo
override this.GetField : string -> System.Reflection.FieldInfo
Public Function GetField (name As String) As FieldInfo
매개 변수
- name
- String
가져올 데이터 필드의 이름을 포함하는 문자열입니다.
반품
지정된 이름을 가진 public 필드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
구현
- 특성
예외
name은 null입니다.
이 Type 개체는 TypeBuilder 메서드가 CreateType() 아직 호출되지 않은 개체입니다.
예제
다음 예제에서는 지정된 클래스의 개체를 가져오 Type 고 필드의 FieldInfo 개체를 가져오고 필드 값을 표시합니다.
using System;
using System.Reflection;
public class MyFieldClassA
{
public string Field = "A Field";
}
public class MyFieldClassB
{
private string field = "B Field";
public string Field
{
get
{
return field;
}
set
{
if (field!=value)
{
field=value;
}
}
}
}
public class MyFieldInfoClass
{
public static void Main()
{
MyFieldClassB myFieldObjectB = new MyFieldClassB();
MyFieldClassA myFieldObjectA = new MyFieldClassA();
Type myTypeA = typeof(MyFieldClassA);
FieldInfo myFieldInfo = myTypeA.GetField("Field");
Type myTypeB = typeof(MyFieldClassB);
FieldInfo myFieldInfo1 = myTypeB.GetField("field",
BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine("The value of the public field is: '{0}'",
myFieldInfo.GetValue(myFieldObjectA));
Console.WriteLine("The value of the private field is: '{0}'",
myFieldInfo1.GetValue(myFieldObjectB));
}
}
open System.Reflection
type MyFieldClassA =
val public Field: string
new () = { Field = "A Field"}
type MyFieldClassB() =
let field = "B Field"
member _.Field
with get () = field
let myFieldObjectB = MyFieldClassB()
let myFieldObjectA = MyFieldClassA()
let myTypeA = typeof<MyFieldClassA>
let myFieldInfo = myTypeA.GetField "Field"
let myTypeB = typeof<MyFieldClassB>
let myFieldInfo1 = myTypeB.GetField("field", BindingFlags.NonPublic ||| BindingFlags.Instance)
printfn $"The value of the public field is: '{myFieldInfo.GetValue myFieldObjectA}'"
printfn $"The value of the private field is: '{myFieldInfo1.GetValue myFieldObjectB}'"
Imports System.Reflection
Public Class MyFieldClassA
Public Field As String = "A Field"
End Class
Public Class MyFieldClassB
Private myField As String = "B Field"
Public Property Field() As String
Get
Return myField
End Get
Set(ByVal Value As String)
If myField <> value Then
myField = value
End If
End Set
End Property
End Class
Public Class MyFieldInfoClass
Public Shared Sub Main()
Dim myFieldObjectB As New MyFieldClassB()
Dim myFieldObjectA As New MyFieldClassA()
Dim myTypeA As Type = GetType(MyFieldClassA)
Dim myFieldInfo As FieldInfo = myTypeA.GetField("Field")
Dim myTypeB As Type = GetType(MyFieldClassB)
Dim myFieldInfo1 As FieldInfo = myTypeB.GetField("myField", _
BindingFlags.NonPublic Or BindingFlags.Instance)
Console.WriteLine("The value of the public field is: '{0}'", _
myFieldInfo.GetValue(myFieldObjectA))
Console.WriteLine("The value of the private field is: '{0}'", _
myFieldInfo1.GetValue(myFieldObjectB))
End Sub
End Class
설명
검색에서 name은 대/소문자를 구분합니다. 검색에는 공용 정적 및 공용 인스턴스 필드가 포함됩니다.
현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 적절한 형식 인수로 대체된 형식 매개 변수를 반환 FieldInfo 합니다.
현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 필드를 검색합니다.
추가 정보
적용 대상
GetField(String, BindingFlags)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 바인딩 제약 조건을 사용하여 지정된 필드를 검색합니다.
public:
abstract System::Reflection::FieldInfo ^ GetField(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)]
public abstract System.Reflection.FieldInfo? GetField(string name, System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.FieldInfo GetField(string name, System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.FieldInfo? GetField(string name, System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)>]
abstract member GetField : string * System.Reflection.BindingFlags -> System.Reflection.FieldInfo
abstract member GetField : string * System.Reflection.BindingFlags -> System.Reflection.FieldInfo
Public MustOverride Function GetField (name As String, bindingAttr As BindingFlags) As FieldInfo
매개 변수
- name
- String
가져올 데이터 필드의 이름을 포함하는 문자열입니다.
반품
지정된 요구 사항과 일치하는 필드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
구현
- 특성
예외
name은 null입니다.
예제
다음은 지정된 클래스의 Type 개체를 가져오고, 지정된 바인딩 플래그와 일치하는 필드의 개체를 가져오 FieldInfo 고, 필드 값을 표시하는 예제입니다.
using System;
using System.Reflection;
public class MyFieldClassA
{
public string Field = "A Field";
}
public class MyFieldClassB
{
private string field = "B Field";
public string Field
{
get
{
return field;
}
set
{
if (field!=value)
{
field=value;
}
}
}
}
public class MyFieldInfoClass
{
public static void Main()
{
MyFieldClassB myFieldObjectB = new MyFieldClassB();
MyFieldClassA myFieldObjectA = new MyFieldClassA();
Type myTypeA = typeof(MyFieldClassA);
FieldInfo myFieldInfo = myTypeA.GetField("Field");
Type myTypeB = typeof(MyFieldClassB);
FieldInfo myFieldInfo1 = myTypeB.GetField("field",
BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine("The value of the public field is: '{0}'",
myFieldInfo.GetValue(myFieldObjectA));
Console.WriteLine("The value of the private field is: '{0}'",
myFieldInfo1.GetValue(myFieldObjectB));
}
}
open System.Reflection
type MyFieldClassA =
val public Field: string
new () = { Field = "A Field"}
type MyFieldClassB() =
let field = "B Field"
member _.Field
with get () = field
let myFieldObjectB = MyFieldClassB()
let myFieldObjectA = MyFieldClassA()
let myTypeA = typeof<MyFieldClassA>
let myFieldInfo = myTypeA.GetField "Field"
let myTypeB = typeof<MyFieldClassB>
let myFieldInfo1 = myTypeB.GetField("field", BindingFlags.NonPublic ||| BindingFlags.Instance)
printfn $"The value of the public field is: '{myFieldInfo.GetValue myFieldObjectA}'"
printfn $"The value of the private field is: '{myFieldInfo1.GetValue myFieldObjectB}'"
Imports System.Reflection
Public Class MyFieldClassA
Public Field As String = "A Field"
End Class
Public Class MyFieldClassB
Private myField As String = "B Field"
Public Property Field() As String
Get
Return myField
End Get
Set(ByVal Value As String)
If myField <> value Then
myField = value
End If
End Set
End Property
End Class
Public Class MyFieldInfoClass
Public Shared Sub Main()
Dim myFieldObjectB As New MyFieldClassB()
Dim myFieldObjectA As New MyFieldClassA()
Dim myTypeA As Type = GetType(MyFieldClassA)
Dim myFieldInfo As FieldInfo = myTypeA.GetField("Field")
Dim myTypeB As Type = GetType(MyFieldClassB)
Dim myFieldInfo1 As FieldInfo = myTypeB.GetField("myField", _
BindingFlags.NonPublic Or BindingFlags.Instance)
Console.WriteLine("The value of the public field is: '{0}'", _
myFieldInfo.GetValue(myFieldObjectA))
Console.WriteLine("The value of the private field is: '{0}'", _
myFieldInfo1.GetValue(myFieldObjectB))
End Sub
End Class
설명
다음 표에서는 형식을 반영할 때 메서드에서 반환되는 기본 클래스의 Get 멤버를 보여 줍니다.
| 멤버 형식 | Static | 비정적 |
|---|---|---|
| 생성자 | No | No |
| Field | No | Yes. 필드는 항상 이름과 서명을 기준으로 숨겨집니다. |
| 이벤트 | 적용할 수 없음 | 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요. |
| Method | No | Yes. 메서드(가상 및 가상이 아닌 메서드)는 이름으로 숨기기 또는 이름과 서명으로 숨기기가 가능합니다. |
| 중첩 타입 | No | No |
| 재산 | 적용할 수 없음 | 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요. |
이름별 및 서명에 의한 숨기기는 사용자 지정 한정자, 반환 형식, 매개 변수 형식, 센티넬 및 비관리 호출 규칙을 포함하여 서명의 모든 부분을 고려합니다. 이진 비교입니다.
리플렉션의 경우 속성과 이벤트는 이름 및 서명별로 숨겨집니다. 기본 클래스에 get 및 set 접근자가 모두 있는 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기며 기본 클래스의 setter에 액세스할 수 없습니다.
사용자 지정 특성은 공용 형식 시스템의 일부가 아닙니다.
다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 필드를 정의할 수 있습니다.
반환을 얻으려면
BindingFlags.Instance또는BindingFlags.Static중 하나를 지정해야 합니다.검색에 공용 필드를 포함하도록 지정
BindingFlags.Public합니다.검색에 public이 아닌 필드(즉, 프라이빗, 내부 및 보호된 필드)를 포함하도록 지정
BindingFlags.NonPublic합니다.위계에서
BindingFlags.FlattenHierarchy및public정적 멤버를 포함하도록protected을(를) 지정하세요. 상속된 클래스에서는private정적 멤버가 포함되지 않습니다.
다음 BindingFlags 한정자 플래그를 사용하여 검색 작동 방식을 변경할 수 있습니다.
BindingFlags.IgnoreCase의 대소문자를 무시합니다.BindingFlags.DeclaredOnly단순히 상속된 필드가 아니라 선언 Type된 필드만 검색합니다.
자세한 내용은 System.Reflection.BindingFlags을 참조하세요.
현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 적절한 형식 인수로 대체된 형식 매개 변수를 반환 FieldInfo 합니다.
현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 필드를 검색합니다.