如果存在装配C#的条件编译 [英] C# conditional compilation if assembly exists
问题描述
我有可能或可能不存在的基准的项目。我有一个使用的参考代码,我想如果大会存在只编译它。我沿着线思维的内容:
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 something along the lines of:
#if ASSEMBLY_EXISTS
AssemblyClass.DoSomething();
#endif
根据需要,我可以在顶部和注释/取消注释放的#define ,但我更喜欢它是否能不知怎么竟知道的,如果它有没有我的人工干预,这使我相信,#如果不会为这种情况下工作。有另一种方式的基础上是否存在装配条件编译
I could put a #define at the top and comment/uncomment as needed, but I'd prefer if it could just somehow know if it's there without my manual intervention, which leads me to believe that #if won't work for this situation. Is there another way to conditionally compile based on whether an assembly exists?
推荐答案
也许里面MSBUILD条件做;
Maybe do it with a condition inside MSBUILD;
它看起来像它
<PropertyGroup>
<DefineConstants Condition="Exists('my.dll') ">$(DefineConstants);DLLEXISTS</DefineConstants>
</PropertyGroup>
和应该去很远倒在你的.csproj的文件。
and should go quite far down in your .csproj file.
这读取,大致为通过附加DLLEXISTS重新定义的常量,如果my.dll存在
This reads, roughly, as "redefine the constants by appending DLLEXISTS, if my.dll exists"
现在,你应该能够做到
#if DLLEXISTS
// your stuff here
#endif
您可能需要反复折腾存在表达式找到一个合适的相对路径。
You might need to fiddle around with the EXISTS expression to find an appropriate relative path.
这篇关于如果存在装配C#的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!