Retrieving information about a field in a class.

  • Thread starter Thread starter Nathan Bullock
  • Start date Start date
N

Nathan Bullock

Hello Everyone,

Is there anyway to get the fieldinfo object for a specified field in a
class. Something better than telling what its name is, since this
isn't checked by the compiler.

Something maybe like this:

class Foo
{
public int foo;

public Foo()
{
FieldInfo fi = FieldInfo(foo);
// or fi = foo.Field();
Console.Writeline("{0}", fi.Name);
}
}

Nathan Bullock
 
Hello Nathan,

You can query a type instance for all fields it has, and then iterate
through the returned array of FieldInfo objects. But I'm afraid there's no
way to obtain the FieldInfo instance direct from a field variable like you
have suggested.
 
Back
Top