編譯器錯誤 CS1705

組件 'AssemblyName1' 使用的 'TypeName' 版本高於所參考的組件 'AssemblyName2'

您正在存取的類型,其版本號碼高於所參考組件中的版本號碼。 通常這種錯誤是因為意外使用同一組件的兩種版本所造成。

例如,假設您有 Asmb1 和 Asmb2 這兩個組件。 組件 Asmb1 參考組件 Asmb2 的 1.0 版。 組件 Asmb1 也使用組件 Asmb2 的 2.0 版中所加入的類別 MyClass。 由於編譯器在繫結參考方面有統一規則,因此對 MyClass 2.0 版的參考不能使用 1.0 版。

範例

下列詳細的範例包含四個程式碼模組:

  • 除了版本屬性之外完全相同的兩個 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 建立的 DLL (1.0 版)。 AssemblyB 會參考 CS1705b.cs 建立的 DLL (2.0 版)。 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 建立的 DLL (1.0 版)。 不過,在 Main 方法中,程式碼會從 CS1705c.cs 存取 ClassCClassC 會使用 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

    雖然程式會在您使用此命令時編譯,但是還不會執行。 若要讓程式執行,您可以提供包含使用 <assemblyIdentity><codeBase> 子元素之 <dependentAssembly> 元素的應用程式組態檔,來指定 DLL 1.0 版的位置。 如需設定檔的詳細資訊,請參閱設定應用程式

另請參閱