コンパイラ エラー CS1705

アセンブリ '<アセンブリ名 1>' は、参照されているアセンブリ '<アセンブリ名 2' よりも新しいバージョンを含む '<型名>' を使用します

参照アセンブリのバージョン番号より新しいバージョン番号の型にアクセスしています。 通常、このエラーは、同じアセンブリ内の 2 バージョンの誤った使用によって発生します。

たとえば、2 種類のアセンブリ、Asmb1 と Asmb2 があるとします。 アセンブリ Asmb1 はアセンブリ Asmb2 のバージョン 1.0 を参照します。 アセンブリ Asmb1 は、バージョン 2.0 のアセンブリ Asmb2 に追加されたクラス MyClass も使用します。 コンパイラにバインディング参照の統一規則があり、バージョン 2.0 の MyClass への参照をバージョン 1.0 で解決することはできません。

次の例は、4 種類のコード モジュールで構成されます。

  • バージョン属性以外はまったく同じ 2 つの DLL。

  • 最初の 2 つを参照する 3 番目の 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() {}  
}  

次のコードは AssemblyVersionAttribute 属性で指定されたアセンブリのバージョン 2.0 を定義します。

// 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() { }  
}  

次のコードは前のコードで定義されている 2 種類の DLL バージョンを参照しています。 AssemblyA は CS1705a.cs (バージョン 1.0) で作成された DLL を参照しています。 AssemblyB は CS1705b.cs (バージョン 2.0) で作成された DLL を参照しています。 ClassC には、2 つのメソッドが定義されています。 1 つ目の Return1A は、DLL のバージョン 1.0 の Class1 のエイリアスである型 Class1A のオブジェクトを返します。 2 つ目の Return1B は、DLL のバージョン 2.0 の Class1 のエイリアスである型 Class1B のオブジェクトを返します。 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.dll のバージョン番号が、CS1705.dll の参照されるバージョンよりも後のバージョン番号なので、コンパイラ エラー CS1705 が発生します。

// 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 を使用するようにコードを更新する。

  • 次のコマンドを使用してコンパイルすることで、バージョン 2.0 の DLL への参照を CS1705d.cs に追加する。

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

    このコマンドを使用するとプログラムはコンパイルされますが、まだ実行しません。 プログラムを実行するには、<assemblyIdentity> および <codeBase> 子要素を使用する <dependentAssembly> 要素を含むアプリケーション構成ファイルに、バージョン 1.0 の DLL の場所を指定します。 構成ファイルの詳細については、アプリの構成に関するページを参照してください。

関連項目