编译器错误 CS1705

您所在的位置:网站首页 代号zny 编译器错误 CS1705

编译器错误 CS1705

2023-11-15 10:36| 来源: 网络整理| 查看: 265

编译器错误 CS1705 项目 04/07/2023

程序集“AssemblyName1”使用的“TypeName”版本高于引用的程序集“AssemblyName2”使用的版本

您当前访问的类型的版本号高于所引用的程序集的版本号。 此错误通常由意外使用相同程序集的两个版本导致。

例如,假设您有两个程序集,Asmb1 和 Asmb2。 程序集 Asmb1 引用程序集 Asmb2 1.0 版。 程序集 Asmb1 还使用已添加到程序集 Asmb2 2.0 版的类 MyClass。 编译器拥有统一的引用绑定规则,对 2.0 版中的 MyClass的引用无法由 1.0 版满足。

示例

下面更为详细的示例由四个代码模块构成:

两个 DLL,它们除版本属性外,其他方面都相同。

引用前两个 DLL 的第三个 DLL。

仅引用相同 DLL 的 1.0 版但访问 2.0 版中的类的客户端。

下面的代码创建这两个相同 DLL 中的第一个。 有关如何生成密钥文件的信息,请参阅 KeyFile(C# 编译器选项)。

// CS1705a.cs // Compile by using the following command: // csc /target:library /out:C:\\CS1705.dll /keyfile:mykey.snk CS1705a.cs // The DLL is created in the C:\ directory. // The AssemblyVersion attribute specifies version 1.0 for this DLL. [assembly:System.Reflection.AssemblyVersion("1.0")] public class Class1 { public void Method1() {} }

下面的代码定义程序集 2.0 版,如特性 AssemblyVersionAttribute 所指定。

// CS1705b.cs // Compile by using the following command: // csc /target:library /out:CS1705.dll /keyfile:mykey.snk CS1705b.cs // The DLL is created in the current directory. // The AssemblyVersion attribute specifies version 2.0 for this DLL. [assembly:System.Reflection.AssemblyVersion("2.0")] public class Class1 { public void Method1() { } }

下面的代码引用前面的代码中定义的两个 DLL 版本。 AssemblyA 引用由 CS1705a.cs(1.0 版)创建的 DLL。 AssemblyB 引用由 CS1705b.cs(2.0 版)创建的 DLL。 在 ClassC 中定义两个方法。 第一个方法 Return1A 返回 Class1A 类型的对象,该对象是 DLL 1.0 版的中 Class1 的别名。 第二个方法 Return1B 返回 Class1B 类型的对象,该对象是 DLL 2.0 版中的 Class1 的别名。 Return1A 的定义对版本 1.0 形成依赖关系;Return1B 的定义对版本 2.0 形成依赖关系。

// CS1705c.cs // Compile by using the following command. AssemblyA refers to the DLL created by // CS1705a.cs (version 1.0). AssemblyB refers to the DLL created by CS1705b.cs // (version 2.0). // csc /target:library /r:AssemblyA=C:\\CS1705.dll /r:AssemblyB=CS1705.dll CS1705c.cs extern alias AssemblyA; extern alias AssemblyB; // Class1A is an alias for type Class1 from VS1705a.cs, which is in version 1.0 // of the assembly. Class1B is an alias for type Class1 from CS1705b.cs, which // is in version 2.0 of the assembly. using Class1A = AssemblyA::Class1; using Class1B = AssemblyB::Class1; // Method Return1A in ClassC returns an object of type Class1A, which is // Class1 from version 1.0 of the DLL. Method Return1B returns an object // of type Class1B, which is Class1 from version 2.0 of the DLL. public class ClassC { // The following line creates a dependency on version 1.0 of CS1705.dll. // This is not the source of the problem when ClassC is accessed from // CS1705d.cs because CS1705d.cs references version 1.0 of the DLL. // Therefore, type Class1A and the assembly have the same version. public static Class1A Return1A() { return new Class1A(); } // The following line creates a dependency on version 2.0 of CS1705.dll. // This causes compiler error CS1705 when ClassC is accessed from // CS1705d.cs, because CS1705d.cs does not reference version 2.0 of the // DLL. Class1B is the alias for Class1 in version 2.0, and CS1705d.cs // references version 1.0. public static Class1B Return1B() { return new Class1B(); } }

下面的代码生成编译器错误 CS1705。 它引用由 CS1705a.cs(1.0 版)创建的 DLL。 但是,在 Main 方法中,代码访问 CS1705c.cs 中的 ClassC。 ClassC 使用在 CS1705b.cs(2.0 版)中定义的类型。 这导致编译器错误 CS1705,因为类型中的 CS1705.dll 的版本号高于所引用的 CS1705.dll 的版本号。

// CS1705d.cs // Compile by using the following command: // csc /reference:C:\\CS1705.dll /reference:CS1705c.dll CS1705d.cs // C:\\CS1705.dll is version 1.0 of the assembly. class Tester { static void Main() { // Return1A returns a type defined in version 1.0. ClassC.Return1A().Method1(); // Return1B returns a type defined in version 2.0. ClassC.Return1B().Method1(); } }

可以通过以下方式之一来解决这个错误:

更新代码,以便所有文件使用相同版本的 DLL。

通过使用以下命令进行编译,将对 DLL 2.0 版的引用添加到 CS1705d.cs 中:

csc /reference:C:\\CS1705.dll /reference:CS1705.dll /reference:CS1705c.dll CS1705d.cs

使用此命令时,虽然程序在进行编译,但它并未运行。 若要使程序运行,可提供应用程序配置文件并在其中包含使用 和 子元素的 元素,指定 DLL 1.0 版的位置。 有关配置文件的详细信息,请参阅配置应用。

另请参阅 外部别名 ::运算符


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3