C#中的Type.GetFields()方法
C#中的Type.GetFields()方法用于获取当前Type的字段。
语法
以下是语法-
public System.Reflection.FieldInfo[] GetFields ();
示例
现在让我们看一个实现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()); } } }
输出结果
这将产生以下输出-
Following are the non-public fields= Int32 TrimHead Int32 TrimTail Int32 TrimBoth Int32 charPtrAlignConst Int32 alignConst
示例
现在让我们来看另一个实现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()); } } }
输出结果
这将产生以下输出-
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