本文整理汇总了C#中System.Reflection.FieldInfo.GetValue方法的典型用法代码示例。如果您正苦于以下问题:C# FieldInfo.GetValue方法的具体用法?C# FieldInfo.GetValue怎么用?C# FieldInfo.GetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.FieldInfo
的用法示例。
在下文中一共展示了FieldInfo.GetValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Reflection;
class Example
{
public static String val = "test";
public static void Main()
{
FieldInfo fld = typeof(Example).GetField("val");
Console.WriteLine(fld.GetValue(null));
val = "hi";
Console.WriteLine(fld.GetValue(null));
}
}
输出:
test hi
示例2: FieldsClass
//引入命名空间
using System;
using System.Reflection;
public class FieldsClass
{
public string fieldA;
public string fieldB;
public FieldsClass()
{
fieldA = "A public field";
fieldB = "Another public field";
}
}
public class Example
{
public static void Main()
{
FieldsClass fieldsInst = new FieldsClass();
// Get the type of FieldsClass.
Type fieldsType = typeof(FieldsClass);
// Get an array of FieldInfo objects.
FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public
| BindingFlags.Instance);
// Display the values of the fields.
Console.WriteLine("Displaying the values of the fields of {0}:",
fieldsType);
for(int i = 0; i < fields.Length; i++)
{
Console.WriteLine(" {0}:\t'{1}'",
fields[i].Name, fields[i].GetValue(fieldsInst));
}
}
}
输出:
Displaying the values of the fields of FieldsClass: fieldA: 'A public field' fieldB: 'Another public field'