This code returns fields that I created but also some system fields (I'm in WPF app) I didn't create myself:
FieldInfo[] fieldInfos;
fieldInfos = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
How to exclude the system fields and keep only my own ?
Update: these fields are not fields I inherited from my own class either.
I assume you have inherited from something other than object
- in which case add DeclaredOnly
to your GetFields
call:
DeclaredOnly
Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered.
So you would have:
FieldInfo[] fieldInfos = this.GetType().GetFields(
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With