0

Now I am reading "CLR via C#" book, and I am interested in following question - Is it possible to change structure of class via attributes like this:

[UseFooAttr(true)]
class A
{
  if (IsDefined(typeof(UseFooAttr)))
    public Foo FooProperty{get; set}
};

But I am not sure, that this approach has some real benefits.

LmTinyToon
  • 4,205
  • 3
  • 22
  • 46

1 Answers1

1

No that's not possible. But you may want to use conditional compilation:

#define UseFooPropertery // define pre-processor symbol
   class A
   {
#if UseFooProperty // check if symbol is defined
       public Foo FooProperty{get; set}
#endif
   }

Instead of the #if/#endif preprocessor directives you can also use the Conditional attribute:

#define UseFooPropertery // define pre-processor symbol
   class A
   {
       [Conditional("UseFooPropertery")]
       public Foo FooProperty{get; set}
   }

You probably want to define the symbol in your project settings instead of putting them in your code.

René Vogt
  • 41,709
  • 14
  • 73
  • 93