weixin_39568083
weixin_39568083
2021-01-04 22:23

Add support for ConditionalAttribute

A .NET feature that is useful is the [Conditional("DEBUG")] attribute.

From MSDN:

Applying ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined. You will get a compilation error in Visual Studio if you apply this attribute to a method that does not return void. Applying ConditionalAttribute to an attribute indicates that the attribute should not be emitted to metadata unless the conditional compilation symbol is defined. Any arguments passed to the method or attribute are still type-checked by the compiler.

该提问来源于开源项目:bridgedotnet/Bridge

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39710462 weixin_39710462 3月前

    Thank you for the feature request!

    点赞 评论 复制链接分享
  • weixin_39739234 weixin_39739234 3月前

    Hi, I'd like to share some details regarding this issue. At first sight it looks pretty simple - what we need is just ConditionalAttribute applicable to methods and classes (Attribute inheritors only), then the corresponding checks should be implemented in Emitter.

    However, there is another thing which should be addressed. MSDN says that there are 3 ways to define conditional symbols: 1. Use compiler command-line options; for example, /define:DEBUG 2. Use environment variables in the operating system shell; for example, set DEBUG=1 3. Use pragmas in the source code, e.g. #define DEBUG

    The things can become more complicated if undefine pragma should be supported as well, e.g. #undefine DEBUG

    I'm going to dive deeper and investigate whether all these techniques can be supported. Hope I'll be able to suggest some solution soon.

    点赞 评论 复制链接分享
  • weixin_39639643 weixin_39639643 3月前

    Hi, please note that pragmas are already supported by Bridge

    点赞 评论 复制链接分享
  • weixin_39710462 weixin_39710462 3月前

    There was an individual issue for System.Diagnostics.Debug - #718. Just for references.

    点赞 评论 复制链接分享