您的位置:首页 > 其它

FieldInfo学习心得

2014-03-27 22:55 148 查看
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为包含该字段的对象。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: