C# Dynamic Attribute Arguments

安稳与你 提交于 2019-11-29 15:12:19

You can provide dynamic custom type information at runtime by implementing the ICustomTypeDescriptor interface - but this is quite a bit of work at not nearly as simple as decorating properties with attributes.

For runtime, I think that you are probably looking at ICustomTypeDescriptor. If it were a compile time decision, you could have used compiler directives:


 #define ISBROWSABLE
 #if ISBROWSABLE
 [your attribute]
 #endif

Michael Freidgeim

You can load value from some config file or database using approach similar to How to set dynamic value in my Attribute by passing class and property names, e.g.

[IsBrowsable("classname", "propertyname")]

However it will be annoying to type as string names, that are obvious and somehow should be able to determined from reflection. You can try to us IL Weaver tools, such as PostSharp or Fody.( I believe, that they capable to do such thinks, but don't have an example just now)☑

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!