C# conditional compilation if assembly exists

后端 未结 2 769
眼角桃花
眼角桃花 2021-01-15 05:53

I have a project with a reference that may or may not exist. I have code that uses that reference and I\'d like to compile it only if the assembly exists. I\'m thinking some

2条回答
  •  心在旅途
    2021-01-15 06:13

    Maybe do it with a condition inside MSBUILD;

    It would look something like it

    
         $(DefineConstants);DLLEXISTS
    
    

    and should go quite far down in your .csproj file.

    This reads, roughly, as "redefine the constants by appending DLLEXISTS, if my.dll exists"

    Now you should be able to do

    #if DLLEXISTS
        // your stuff here
    #endif
    

    You might need to fiddle around with the EXISTS expression to find an appropriate relative path.

提交回复
热议问题