Compiler Error CS1705

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

您要进入一个版本号高于所引用的程序集的版本号的类型。此错误通常由意外使用相同程序集的两个版本引发。

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

下面详细示例由四个代码模块:

  • 两个 DLL,它们除了版本特性外,其他方面都是一样的。

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

  • 引用相同的 DLL的1.0版本的客户,但是,从版本 2.0 访问的类。

下面的代码创建两个相同的 DLL 中的第一个。有关如何生成密钥文件的信息,请参见 /keyfile (C# Compiler Options)

// 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 引用 CS1705 a.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。它引用CS1705 a.cs (版本 1.0)创建的DLL 。但是,在 Main 方法中,代码从CS1705 c.cs 的 ClassC来访问 。 ClassC 使用在CS1705 b.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 版的引用添加到 d.cs CS1705:

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

    虽然使用此命令时程序在编译,则在不运行。为了使程序在运行,则可提供包含使用<assemblyIdentity>和<codeBase> 子元素来指定 DLL1.0 版本位置的 <dependentAssembly> 元素的应用程序配置文件。有关配置文件的更多信息,请参见使用配置文件配置应用

请参阅

外部别名(C# 参考)

:: 运算符(C# 参考)

Command-line Building With csc.exe