5

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";
        }
     }
  }
1
  • Do you want to decide this at compile-time or run-time? Commented Jul 7, 2009 at 16:54

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Do you have an quick example of this or a site that shows how to do this? Thanks
Here is an example that uses the interface to localize a grid. codeguru.com/csharp/csharp/cs_controls/propertygrid/article.php/…
given link broken
3

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

Comments

0

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)☑

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.