Hiding Attributes from Reflection

S

Sascha Dietl

Hi NG,
is there any way to hide an attribute from being reflected through PropertyInfo?
The reason for that question: I got a class with a lot of attributes which
are public and bound to the instance and I want to expose a few to an user
interface but even not all.

thanks for any help

regards
sascha
 
J

Jon Skeet [C# MVP]

Sascha Dietl said:
is there any way to hide an attribute from being reflected through PropertyInfo?
The reason for that question: I got a class with a lot of attributes which
are public and bound to the instance and I want to expose a few to an user
interface but even not all.

Could you clarify whether you're really talking about attributes, or
whether you're really talking about properties?

Do you have control over the user interface in question, by the way?
 
S

Sascha Dietl

Hello Jon,
Do you have control over the user interface in question, by the way?

i'm sorry my mistake i meant properties that i want to hide from reflection.

regards
Sascha
 
J

Jon Skeet [C# MVP]

Sascha Dietl said:
i'm sorry my mistake i meant properties that i want to hide from reflection.

Okay, no problem. You can't prevent the properties from being available
from reflection, but if you apply [Browsable(false)] to the property,
then the normal visual designers won't display the property. Depending
on what UI is displaying the objects, that may be good enough.
 
M

Marc Gravell

Another option if the situation warrants...

Actually, most UI bindings don't (directly) use reflection: they use
the component model [it just happens that the default component model
implementation uses reflection].

You could implement ICustomTypeDescriptor, and return your own
PropertyDescriptor implementation (based for instance on
SimplePropertyDescriptor IIRC).

Again, it depends on the scenario - i.e. whether you need to hide
properties based on the value of other properties... but it is a viable
(and very powerful) alternative.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top