views:

251

answers:

4

Is there an attribute that hides a member (specifically a property) from typeof(MyType).GetProperties() in .net?

I'm looking for a quick fix - i.e. not creating custom attributes etc..

thanks

+7  A: 

No.

Reflection allows one to see everything, including members marked private.

(In the end reflection uses the same metadata that the CLR, including the JIT, uses.)

Richard
A: 

Had it been possible, Microsoft would have been the first to do it themselves for .NET Assemblies :).

Aamir
Given much of the source to .NET is available, that must be a political view.
Richard
Source is available in the first place because of reflection. They know that they can't hide the source.
Aamir
Then again, Microsoft themselves designed reflection. If they wanted to hide their code, they could have.
Tor Haugen
That was probably due to industry pressure. But well, this is a separate discussion.
Aamir
+1  A: 

That particular overload of GetProperties (without parameters) only returns the public properties. So you could mark the property as private/internal. Otherwise, I concur with Richard above.

Kurt Schelfthout
+1  A: 

Have a look into PropertyDescriptor instead of PropertyInfo.

These can be provided by overriding the 2 Properties methods in a derived class of TypeConverter.

With a little bit of ingenuity, you can pretty much make it do anything.

I currently use this to provide a flat list of properties for various cultures and values for translation purposes, and feed this to a PropertyGrid, while the class structure looks like:

class TagName
{
  Culture culture;
  string content;
}

[TypeConverter(typeof(TagConverter))]
class Tag
{
  TagName[] tagNames;
}

Within the PropertyDescriptor you have complete control how values are set and get via a specific PropertyDescriptor instance.

leppie
interesting, thanks
flesh
Very interesting stuff indeed :)
leppie