Is there a framework attribute to hide a member fr

2019-07-06 17:43发布

问题:

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

回答1:

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.)



回答2:

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.



回答3:

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.



回答4:

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