次の方法で共有


コンパイラ エラー CS1705

アセンブリ 'AssemblyName1' では、参照されるアセンブリ 'AssemblyName2' よりも高いバージョンの 'TypeName' が使用されます。

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

たとえば、Asmb1 と Asmb2 という 2 つのアセンブリがあるとします。 アセンブリ 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 の最初の 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 つのメソッドが定義されています。 最初のReturn1Aは、dll のバージョン 1.0 からのClass1AのエイリアスであるClass1型のオブジェクトを返します。 2 つ目のReturn1Bは、バージョン 2.0 の DLL のClass1Bのエイリアスである、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 メソッドでは、コードは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 を使用するようにコードを更新します。

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

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

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

こちらも参照ください