C# conditional compilation if assembly exists

☆樱花仙子☆ 提交于 2019-12-01 10:44:48

Maybe do it with a condition inside MSBUILD;

It would look something like it

<PropertyGroup>
     <DefineConstants Condition="Exists('my.dll') ">$(DefineConstants);DLLEXISTS</DefineConstants>
</PropertyGroup>

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.

No you cannot do this. You cannot define the result of a conditional compilation symbol at compile time.

If you want to get fancy you could write a new program which detects the missing assembly and modifies your source. You can then execute this program in the Pre-build event of your project.

The modification of the source could simply be the addition or removal of your suggested #define at the top of the source files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!