【发布时间】:2013-11-04 22:40:41
【问题描述】:
是否可以在 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
}
【问题讨论】:
-
我不知道你可以在 C# 中使用 #DEFINE 和 #IF 等:我以为只有 C 或 C++
-
@Joe 是的,你可以使用
define和if但你不能这样做(这真的很烦人)#if x > y只有像#if DEBUG这样的简单 if 语句 -
'Create' 是静态方法吗?
-
@AlessandroD'Andria 是的,它是静态的
标签: c# compiler-construction reference