Getting fields of a class through reflection
Posted
by Water Cooler v2
on Stack Overflow
See other posts from Stack Overflow
or by Water Cooler v2
Published on 2010-06-16T11:45:36Z
Indexed on
2010/06/16
11:52 UTC
Read the original article
Hit count: 239
reflection
|c#4.0
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();
}
© Stack Overflow or respective owner