更新时间:2022-05-09 22:42:48
可以使用反射进行。检查字段信息并的的PropertyInfo 例子。
It can be done using Reflection. Check for FieldInfo and PropertyInfo examples.
MSDN示例(修改了一下):
MSDN example (modified a bit):
Type myType = typeof(TestStruct);
// Get the fields of TestStruct.
FieldInfo[] myFieldInfo =
myType.GetFields(
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
Console.WriteLine("\nThe fields of TestStruct are \n");
// Display the field information of TestStruct.
for(int i = 0; i < myFieldInfo.Length; i++)
{
Console.WriteLine("\nName : {0}", myFieldInfo[i].Name);
Console.WriteLine("Declaring Type : {0}", myFieldInfo[i].DeclaringType);
Console.WriteLine("IsPublic : {0}", myFieldInfo[i].IsPublic);
Console.WriteLine("MemberType : {0}", myFieldInfo[i].MemberType);
Console.WriteLine("FieldType : {0}", myFieldInfo[i].FieldType);
Console.WriteLine("IsFamily : {0}", myFieldInfo[i].IsFamily);
}