FieldInfo是反射应用中的类,它表示某个对象公共字段的信息。Type.GetField(string name)的返回类型为FieldInfo。例如知道对象objExample,其一个共有字段名为strName,则objExample.GetType().GetField(strName)可以获得对象中strName字段的信息。另外可以通过FileInfo的GetValue(Object obj)获得obj对象中该字段的值。应用:当知道某对象和该对象的某个公共字段名时,可以通过Type的GetField方法和FieldInfo的GetValue方法获得对象中该字段的值。
在MSDN中GetField(string name)的解释为:
// Summary:
// Searches for the public field with the specified name.
//
// Parameters:
// name:
// The System.String containing the name of the data field to get.
即该方法的作用是查询指定名字的公用字段,其中阐述name是所查询共有字段的名称。
MSDN中对FieldInfo.GetValue(Object obj)的解释为:
// Summary:
// When overridden in a derived class, returns the value of a field supported
// by a given object.
//
// Parameters:
// obj:
// The object whose field value will be returned.
//
// Returns:
// An object containing the value of the field reflected by this instance.
即该方法的作用是返回给定对象支持的字段的值,其中参数obj为包含该字段的对象。