gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:53:41 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com