Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore property when using reflection

Tags:

c#

reflection

i have a public property in my class, how should I mark it (some attribute) to be ignored when following flags are given

BindingFlags.DeclaredOnly | 
BindingFlags.Instance | 
BindingFlags.NonPublic | 
BindingFlags.Public

I need to use method from dll with parameters: object and binding flags, so i need to mark my property somehow to be ignored by this method

like image 872
kosnkov Avatar asked Feb 26 '26 14:02

kosnkov


1 Answers

I think you're confusing the roles of BindingFlags and CustomAttributes. BindingFlags find things based on whether they are private, static, etc.

If you want to ignore a property in your consuming code, you'll need to change that consuming code to ignore properties with a particular custom attribute associated with them.

If you don't have access to the consuming code, there isn't any way to make it not see a property that is there. There may be some workarounds depending on how the code is written, though. For example, if the code is searching based on a specific type argument, you could create an abstract base class to house all the properties you want it to care about, and then extend that class for your actual implementation, adding some extra properties for your convenience.

Or you could create a DTO to represent just the properties you want to convey to the method you're calling, and copy the appropriate properties from your original object into that DTO.

like image 188
StriplingWarrior Avatar answered Feb 28 '26 06:02

StriplingWarrior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!