method to access to the field's class

  • Thread starter Thread starter Vicente Nicolau
  • Start date Start date
V

Vicente Nicolau

Hello,

I wonder if there is some method that returns the fields of a class. Ideally
it should return an array of objets. For exemple, I want to get the custom
fields declared in a form:

Object[] o = this.SuperMethodThatReturnsReferencesToTheFields();

I need to do this becasue during execution time I don't know which field I
must access (and I just need to set it equal to null).

I can do it with lots of 'if', but each time I add a new field I must add a
new if. There should be a cleaner way.

I have used the MemberInfo or FieldInfo class to get the information of the
fields, but I didn't see the way to access the field itself in order to set
null.

Thanks for your ideas
Best wishes
 
I'm presuming you are talking classes rather than Win Forms as you mention
reflection at the bottom.

Get your list of FieldInfo Objects then for each FieldInfo Object call the
SetValue method and pass the object to set the field on and the value to set
the field to. Call Getvalue to get the value.

http://msdn.microsoft.com/en-us/library/6z33zd7h.aspx

It the same with PropertyInfo too albeit for properties rather than fields.
 
solved!
thank you very much =)


Paul said:
I'm presuming you are talking classes rather than Win Forms as you mention
reflection at the bottom.

Get your list of FieldInfo Objects then for each FieldInfo Object call the
SetValue method and pass the object to set the field on and the value to
set the field to. Call Getvalue to get the value.

http://msdn.microsoft.com/en-us/library/6z33zd7h.aspx

It the same with PropertyInfo too albeit for properties rather than
fields.



Vicente Nicolau said:
Hello,

I wonder if there is some method that returns the fields of a class.
Ideally it should return an array of objets. For exemple, I want to get
the custom fields declared in a form:

Object[] o = this.SuperMethodThatReturnsReferencesToTheFields();

I need to do this becasue during execution time I don't know which field
I must access (and I just need to set it equal to null).

I can do it with lots of 'if', but each time I add a new field I must add
a new if. There should be a cleaner way.

I have used the MemberInfo or FieldInfo class to get the information of
the fields, but I didn't see the way to access the field itself in order
to set null.

Thanks for your ideas
Best wishes
 
Back
Top