C# Dynamic Attribute Arguments

10,663

Solution 1

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.

Solution 2

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

Share:
10,663
SwDevMan81
Author by

SwDevMan81

Twitter: @SwDevMan81 Mail: SwDevMan81 at Gmail Feel free to ping me for any questions you have. Job Title: Software Engineer Job Develop: Software for radar systems, Software GUI, real time embedded Current Languages: C#, C++, Java AS: Monroe Community College (Engineering Science) BS: University at Buffalo (Computer Science) MS: University at Buffalo (Computer Science & Engineering) MBA: Syracuse University (Business Analytics) Sites: Design Patterns Salt

Updated on June 27, 2022

Comments

  • SwDevMan81
    SwDevMan81 almost 2 years

    Is there a way to do the following? I see that the Attribute Arguments must be a constant expression, so how would I work around this? If I dont want to load some properties into a datagridview using binding, whats 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";
            }
         }
      }