c# #if 与 Conditional属性宏的区别

文章探讨了C#中#if和Conditional编译宏的区别,指出#if在程序集未定义时会被预编译移除,而Conditional即使未被调用也会保留代码。ILSpy示例展示了这两种情况下的代码处理结果。
摘要由CSDN通过智能技术生成

测试代码

using System;
using System.Diagnostics;

namespace ConsoleApp1
{
    public class TestClass
    {
        [Conditional("Debug1")]
        public static void Func1()
        {
            Console.WriteLine("Conditional 宏");
        }

        public static void Func2()
        {
#if Debug2
            Console.WriteLine("#if 宏");
#endif
        }
    }

    class Hello
    {
        static void Main(string[] args)
        {
            TestClass.Func1();
            TestClass.Func2();
            Console.ReadKey();
        }
    }
}

生成DLL,使用ILSpy打开,可以看到:

使用#if 包住的代码直接被预处理去掉了,而使用Conditional的代码带着属性被保留了下来

这里因为两个宏都没添加到编译宏里,所以,可以看到本程序集调用Func1的代码被预编译去掉了。

新增一个工程,切依赖前面的工程

using ConsoleApp1;
using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            TestClass.Func1();
            TestClass.Func2();
        }
    }
}

同样使用ILSpy查看:

可以看到ConsoleApp2调用Func1的代码被预编译去掉了

结论:

1. 使用#if 包的代码,如果本程序集没定义该宏,则在预编译阶段直接去掉

2.使用Conditional属性的函数,在调用的程序集如果没定义,则会去掉该函数的调用,但是代码是存在的

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#if, #elseif, and #endif are preprocessor directives commonly used in programming languages such as C, C++, and C#. These directives are used to conditionally compile code based on certain conditions. The #if directive allows you to test a condition and include or exclude code based on the result. It is followed by a condition, which can be a defined constant, a macro expression, or a combination of these. If the condition evaluates to true, the block of code following the #if directive is compiled. Otherwise, it is skipped. The #elseif directive is used to test an additional condition if the preceding #if or #elseif condition(s) evaluated to false. It provides an alternative condition to be checked. If the condition evaluates to true, the block of code following the #elseif directive is compiled. Otherwise, it is skipped. The #endif directive marks the end of a conditional block. It is used to close the block of code associated with the most recent #if or #elseif directive. Here's an example usage of these directives in C: ```c #define DEBUG_MODE #if defined(DEBUG_MODE) printf("Debug mode is enabled.\n"); #elif defined(TEST_MODE) printf("Test mode is enabled.\n"); #else printf("No special mode is enabled.\n"); #endif ``` In this example, if the `DEBUG_MODE` macro is defined, the code within the first block will be compiled and executed. If not, it will check for the `TEST_MODE` macro and execute the code within the second block if it is defined. If neither macro is defined, the code within the else block will be executed.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值