C # dynamic attribute arguments

Is there any way to do the following? I see that attribute arguments must be a constant expression, so how could I get around this? If I don't want to load some properties into a datagridview using bindings, then what is the next best alternative?

class TestObj { private bool isBrowsable = false; [Browsable(isBrowsable)] public string String1 { get { return "Foo"; } } [Browsable(isBrowsable)] public string String2 { get { return "Baz"; } } } 
+3
c # properties attributes
source share
3 answers

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

+6
source share

For the runtime, I think you are probably looking at the ICustomTypeDescriptor. If this were a compile-time solution, you could use compiler directives:

 #define ISBROWSABLE #if ISBROWSABLE [your attribute] #endif
#define ISBROWSABLE #if ISBROWSABLE [your attribute] #endif 
+3
source share

You can load a value from some configuration file or database using an approach similar to How to set a dynamic value in my attribute by passing class and property names, for example.

[IsBrowsable ("file_name", "file_name")]

However, it will be unpleasant to enter line names that are obvious and somehow need to be able to determine from reflection. You can try using IL Weaver tools like PostSharp or Fody. (I believe that they are able to make such thoughts, but now they have no example) β˜‘

0
source share

All Articles