是否可以在 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/