views:

1209

answers:

2

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";
        }
     }
  }
+3  A: 

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.

Daniel Brückner
Do you have an quick example of this or a site that shows how to do this? Thanks
SwDevMan81
Here is an example that uses the interface to localize a grid. http://www.codeguru.com/csharp/csharp/cs_controls/propertygrid/article.php/c4795
Daniel Brückner
Another one. http://www.codeproject.com/KB/tabs/customizingcollectiondata.aspx
Daniel Brückner
+1  A: 

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

JMarsch