程序集“AssemblyName1”使用版本高于引用的程序集“AssemblyName2”的“TypeName”。
访问的类型的版本号高于所引用程序集中的版本号。 通常,此错误是由意外使用同一程序集的两个版本引起的。
例如,假设你有两个程序集:Asmb1 和 Asmb2。 程序集 Asmb1 引用程序集 Asmb2 版本 1.0。 程序集 Asmb1 还使用在版本 2.0 中添加到程序集 Asmb2 的类 MyClass 。 编译器有用于绑定引用的统一规则,在版本 2.0 中对MyClass的引用不能由版本 1.0 来满足。
例子
以下更详细的示例包含四个代码模块:
- 除了版本属性之外,两个 DLL 相同。
- 引用前两个 DLL 的第三个 DLL。
- 仅引用版本 1.0 的相同 DLL,但却从版本 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 。 对版本 1.0 创建依赖项的定义 Return1A ;创建对版本 2.0 的依赖项的定义 Return1B 。
// 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虽然程序在使用此命令时编译,但它仍然不会运行。 若要使程序能够运行,可以提供一个应用程序配置文件,其中包含一个
<dependentAssembly>元素,该元素使用<assemblyIdentity>和<codeBase>子元素来指定 DLL 版本 1.0 的位置。 有关配置文件的详细信息,请参阅 “配置应用”。