开发者

Getting fields of a class through reflection

I've done it a gazillion times in the past and successfully so. This time, I'm suffering from lapses of amnesia.

So, I am just trying to get the fields on an object. It is an embarrassingly simple and stupid piece of code that I am writing in a test solution before I do something really useful in production code.

Strangely, the GetFieldsOf method reports a zero length array on the "Amazing" class. Help.

class Amazing
{
    private NameValueCollection _nvc;
    开发者_如何学运维protected NameValueCollection _myDict;
}


private static FieldInfo[] GetFieldsOf(string className, 
        string nameSpace = "SomeReflection")
    {
        Type t;

        return (t = Assembly.GetExecutingAssembly().GetType(
             string.Format("{0}.{1}", nameSpace, className)
             )) == null ? null : t.GetFields();
    }


Have a look at BindingFlags.
Try to set at least BindingFlags.Instance | BindingFlags.NonPublic in your GetFields() call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜