c# - 检查编译时是否存在引用

标签 c# compiler-construction reference

是否可以在 C# 编译时检查项目中是否存在引用?

例如;

public void myMethod()
{
    #if REVIT_DLL_2014
       TopographySurface.Create(vertices); // This function only exists in Revit2014.dll
       // So I will get a compiler if another DLL is used 
       // ie, Revit2013.dll, because this method (Create) wont exist
    #else // using Revit2013.dll
       // use alternate method to create a surface
    #endif
}

我想要避免的是维护 2 个单独的 C# 项目(即版本 2013 和版本 2014),因为除了 1 个功能之外,它们几乎在所有方面都是相同的。

我想我最后的手段可能是(但如果上述功能是可能的那就更好了):

#define USING_REVIT_2014

public void myMethod()
{
    #if USING_REVIT_2014
       TopographySurface.Create(vertices); // This function only exists in Revit2014.dll
       // So I will get a compiler if another DLL is used because this method (Create) wont exist
    #else // using Revit2013.dll
       // use alternate method to create a surface
    #endif
}

最佳答案

在运行时而不是编译时进行检测。

if (Type.GetType("Full.Name.Space.To.TopographySurface") != null) {
    TopographySurface.Create(vertices);
}
else {
    // use alternate method to create a surface
}

这假设只要定义了TopographySurface,那么Create就存在。

关于c# - 检查编译时是否存在引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19778667/

相关文章:

c# - 我只想让我的客户向我的服务器打个招呼,但不能

c - 什么是最简单的 MAKEFILE 我可以为单个 C 文件工作?

c++ - 删除不完整类型在 C++ 中不是错误时,是否存在真实案例?

C# WebBrowser CSS 悬停样式不起作用(尽管配置了浏览器仿真)

c# - 调用异步回调但有时不执行

rust - 如何在Rust中将ref转换为en16的枚举值(基本枚举类型)?

java - 如何使用Java 8的函数引用?

html - Firebug 中的网络选项卡不显示任何文件

c# - 使用 C# 在 TextBox 中准备语句查询

haskell - GHC 内部结构 : is there C implementation of the type system?