[英]Create Pre Processor Directive based on conditional statement in C#
I am working in .NET Framework and am using C#. I am referencing external dlls.我在 .NET 框架中工作,正在使用 C#。我正在引用外部 dll。 One particular method in available only if the dlls version is 12 or up.
仅当 dll 版本为 12 或更高版本时,一种特定方法才可用。 Is there a way to condition code compilation based on the version of dlls.
有没有一种方法可以根据 dll 的版本来调节代码编译。
I can get the version of the dlls, but if it is on version 11 or lower, how can I restrict a chuck of code from compiling.我可以获得 dll 的版本,但如果它是版本 11 或更低版本,我如何限制一段代码的编译。
Below is the code I'm currently using, when I refer dlls of version 11 or lower, the GetUnsortedFaces()
shows error and it won't let me compile.下面是我目前正在使用的代码,当我引用版本 11 或更低版本的 dll 时,
GetUnsortedFaces()
显示错误并且不允许我编译。
List<IVctFace> IVctEdge.AllFaces()
{
Face[] faces;
Version version = System.Reflection.Assembly.GetAssembly(typeof(Edge)).GetName().Version;
faces = this.Edge.GetFaces();
if (version.Major > 11)
{
faces = this.Edge.GetUnsortedFaces();
}
List<IVctFace> vctFaces = new List<IVctFace>();
foreach (Face face in faces)
{
vctFaces.Add(new VctFace(face, this.Parent.Parent));
}
return vctFaces;
}
I do know about #define
but they needs to be created outside the namespace.我知道
#define
但它们需要在命名空间之外创建。
I have a suggestion: declare a dynamic variable to reference the Edge object (see https://learn.microsoft.com/en-us/do.net/csharp/programming-guide/types/using-type-dynamic ) You can then call the GetUnsortedFaces function inside a try..catch sequence or use reflection to test whether the GetUnsortedFaces function is implemented by the Edge object before calling that function.我有一个建议:声明一个动态变量来引用Edge object(参见https://learn.microsoft.com/en-us/do.net/csharp/programming-guide/types/using-type-dynamic )你可以然后在 try..catch 序列中调用 GetUnsortedFaces function 或使用反射来测试 GetUnsortedFaces function 在调用 function 之前是否由 Edge object 实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.