Is there a structure attribute to hide the element from reflection in .Net?

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

I am looking for a quick fix, i.e. Do not create custom attributes, etc.

thanks

+3
source share
4 answers

No.

Reflection allows you to see everything, including members marked as private.

(At the end of the reflection, the same metadata that the CLR uses, including JIT) is used.

+8
source

This special GetProperties overload (without parameters) returns only public properties. Thus, you can mark a property as private / internal. Otherwise, I agree with Richard above.

+1
source

See PropertyDescriptor instead of PropertyInfo.

They can be provided by overriding two property methods in the TypeConverter derived class.

With a little ingenuity, you can pretty much get it all done.

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

 class TagName { Culture culture; string content; } [TypeConverter(typeof(TagConverter))] class Tag { TagName[] tagNames; } 

In PropertyDescriptor, you have complete control over how values ​​are set and obtained through a specific instance of PropertyDescriptor.

+1
source

If it were possible, Microsoft would be the first to do this for .NET Assemblies :).

-1
source

Source: https://habr.com/ru/post/1369055/


All Articles