Is there a framework attribute to hide a member fr

2019-07-06 17:09发布

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

4条回答
趁早两清
2楼-- · 2019-07-06 17:32

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

查看更多
叼着烟拽天下
3楼-- · 2019-07-06 17:35

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.

查看更多
神经病院院长
4楼-- · 2019-07-06 17:41

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

查看更多
不美不萌又怎样
5楼-- · 2019-07-06 17:50

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.

查看更多
登录 后发表回答