裝配 'AssemblyName1' 使用「TypeName」,其版本高於參考的裝配 'AssemblyName2'。
你正在存取一個版本號高於參考組件版本號的型別。 通常,這種錯誤是因為誤用了同一組件的兩個版本所造成的。
舉例來說,假設你有兩個組裝體,Asmb1 和 Asmb2。 Assembly Asmb1 參考了 assembly Asmb2 的版本 1.0。 Assembly Asmb1 也使用了在 2.0 版本中加入的 assembly Asmb2 類別 MyClass 。 編譯器有綁定參考的統一規則,且版本 2.0 中對 的引用 MyClass 無法被版本 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(版本 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 方法中,程式碼從 ClassC 存取 CS1705c.cs。
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。
可使用以下指令編譯,為 CS1705d.cs 新增 DLL 2.0 版本的參考:
csc /reference:C:\\CS1705.dll /reference:CS1705.dll /reference:CS1705c.dll CS1705d.cs雖然使用此指令時程式會編譯,但仍無法執行。 為了讓程式能執行,你可以提供一個應用程式設定檔,其中包含
<dependentAssembly>一個元素<assemblyIdentity>,該元素使用 和<codeBase>子元素來指定 DLL 1.0 版本的位置。 欲了解更多關於設定檔的資訊,請參閱 「配置應用程式」。