Phương thức Type.GetFields () trong C # được sử dụng để lấy các trường của Kiểu hiện tại.
Cú pháp
Sau đây là cú pháp -
public System.Reflection.FieldInfo[] GetFields ();
Ví dụ
Bây giờ chúng ta hãy xem một ví dụ để triển khai phương thức Type.GetFields () -
using System;
using System.Reflection;
public class Demo {
public static void Main(){
Type type = typeof(System.String);
FieldInfo [] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
Console.WriteLine ("Following are the non-public fields=");
foreach (FieldInfo myField in fields){
Console.WriteLine(myField.ToString());
}
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
Following are the non-public fields= Int32 TrimHead Int32 TrimTail Int32 TrimBoth Int32 charPtrAlignConst Int32 alignConst
Ví dụ
Bây giờ chúng ta hãy xem một ví dụ khác để triển khai phương thức Type.GetFields () -
using System;
using System.Reflection;
public class Demo {
public static void Main(){
Type type = typeof(System.Char);
FieldInfo [] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
Console.WriteLine ("\nFollowing are the non-public fields=");
foreach (FieldInfo myField in fields){
Console.WriteLine(myField.ToString());
}
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
Following are the non-public fields= Byte[] categoryForLatin1 Int32 UNICODE_PLANE00_END Int32 UNICODE_PLANE01_START Int32 UNICODE_PLANE16_END Int32 HIGH_SURROGATE_START Int32 LOW_SURROGATE_END