Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查编译时是否存在引用_C#_Compiler Construction_Reference - Fatal编程技术网

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

C# 检查编译时是否存在引用,c#,compiler-construction,reference,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.dl

是否可以在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
}
我想要避免的是要维护两个独立的C#项目(即2013版和2014版),因为除了一个特性外,它们几乎在所有方面都是相同的

我想我最后的办法可能是(但如果上述功能可行的话会更好):


我取决于你是使用晚投标还是早投标。 后期绑定: -无编译时间警告/错误 -只有运行时错误

提前投标: -编译器错误
-运行时错误取决于您使用的是延迟投标还是提前投标。 后期绑定: -无编译时间警告/错误 -只有运行时错误

提前投标: -编译器错误
-运行时错误

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

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

这假设只要定义了
地形表面
,就存在
创建

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

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


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

我不知道你可以使用#DEFINE and#IF,e.t.c在c中:我以为只有c或c++@Joe是的,你可以使用
DEFINE
IF
,但你不能这样做(这真的很烦人)
#if x>y
如果语句像
#if DEBUG
'Create'是一个静态方法?@alesandrod'Andria是的,它是静态的。我不知道你可以在c中使用#DEFINE和#if,e.t.c;:我以为只有c或c++@Joe是的,你可以使用
DEFINE
if
,但是你不能这样做(这真的很烦人)
#if x>y
仅当像
#if DEBUG
'Create'这样的语句是一个静态方法时才简单?@alesandrod'Andria是它的staticAnd?它是如何回答这个问题的?@lauCosma感谢您的回答,但任何一种解决方案都会涉及运行时错误。我想我可以在运行时检查引用是否存在?但如果可能的话,最好在编译时执行。请检查以下帖子:您可以尝试捕获引用。。。但这仍然是在运行时。。。所以我认为到目前为止最好的解决方案是运行时错误?它是如何回答这个问题的?@lauCosma感谢您的回答,但任何一种解决方案都会涉及运行时错误。我想我可以在运行时检查引用是否存在?但如果可能的话,最好在编译时执行。请检查以下帖子:您可以尝试捕获引用。。。但这仍然是在运行时。。。所以我认为目前为止最好的解决方案是运行时错误。@JakeM-然后您可以使用反射来检查类型是否存在。请参阅更新的答案。我很想把它放在一个封装类中,这样您就不会因为类型检查而浪费代码库。此外,我会将封装类命名为相同的:
TopographySurface
,仅在您自己的名称空间中。这样,当您决定放弃对2013的支持时,只需使用语句更改您的
。@JakeM-然后您就可以使用反射来检查类型是否存在。请参阅更新的答案。我很想把它放在一个封装类中,这样您就不会因为类型检查而浪费代码库。此外,我会将封装类命名为相同的:
TopographySurface
,仅在您自己的名称空间中。这样,当您决定放弃对2013年的支持时,只需使用
语句更改您的