AppDomain.AssemblyResolve AppDomain.AssemblyResolve AppDomain.AssemblyResolve AppDomain.AssemblyResolve Event

定義

アセンブリの解決が失敗したときに発生します。Occurs when the resolution of an assembly fails.

public:
 virtual event ResolveEventHandler ^ AssemblyResolve;
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public event ResolveEventHandler AssemblyResolve;
member this.AssemblyResolve : ResolveEventHandler 
Public Custom Event AssemblyResolve As ResolveEventHandler 

実装

属性

次のサンプルでは、AssemblyResolveイベント。The following sample demonstrates the AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。For this code example to run, you must provide the fully qualified assembly name. 完全修飾アセンブリ名を取得する方法については、次を参照してください。アセンブリ名します。For information about how to obtain the fully qualified assembly name, see Assembly Names.

public ref class MyType
{
public:
    MyType()
    {
        Console::WriteLine();
        Console::WriteLine("MyType instantiated!");
    }
};

class Test
{
public:
    static void Main()
    {
        AppDomain^ currentDomain = AppDomain::CurrentDomain;

        // This call will fail to create an instance of MyType since the
        // assembly resolver is not set
        InstantiateMyTypeFail(currentDomain);

        currentDomain->AssemblyResolve += gcnew ResolveEventHandler(&Test::MyResolveEventHandler);

        // This call will succeed in creating an instance of MyType since the
        // assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain);

        // This call will succeed in creating an instance of MyType since the
        // assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain);
    }

private:
    static void InstantiateMyTypeFail(AppDomain^ domain)
    {
        // Calling InstantiateMyType will always fail since the assembly info
        // given to CreateInstance is invalid.
        try
        {
            // You must supply a valid fully qualified assembly name here.
            domain->CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
        }
        catch (Exception^ e)
        {
            Console::WriteLine();
            Console::WriteLine(e->Message);
        }
    }

    static void InstantiateMyTypeSucceed(AppDomain^ domain)
    {
        try
        {
            String^ asmname = Assembly::GetCallingAssembly()->FullName;
            domain->CreateInstance(asmname, "MyType");
        }
        catch (Exception^ e)
        {
            Console::WriteLine();
            Console::WriteLine(e->Message);
        }
    }

    static Assembly^ MyResolveEventHandler(Object^ sender, ResolveEventArgs^ args)
    {
        Console::WriteLine("Resolving...");
        return MyType::typeid->Assembly;
    }
};

int main()
{
    Test::Main();
}
public class MyType
{
    public MyType()
    {
        Console.WriteLine();
        Console.WriteLine("MyType instantiated!");
    }
}

class Test
{
    public static void Main()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;

        // This call will fail to create an instance of MyType since the
        // assembly resolver is not set
        InstantiateMyTypeFail(currentDomain);

        currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

        // This call will succeed in creating an instance of MyType since the
        // assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain);

        // This call will succeed in creating an instance of MyType since the
        // assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain);
    }

    private static void InstantiateMyTypeFail(AppDomain domain)
    {
        // Calling InstantiateMyType will always fail since the assembly info
        // given to CreateInstance is invalid.
        try
        {
            // You must supply a valid fully qualified assembly name here.
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
        }
        catch (Exception e)
        {
            Console.WriteLine();
            Console.WriteLine(e.Message);
        }
    }

    private static void InstantiateMyTypeSucceed(AppDomain domain)
    {
        try
        {
            string asmname = Assembly.GetCallingAssembly().FullName;
            domain.CreateInstance(asmname, "MyType");
        }
        catch (Exception e)
        {
            Console.WriteLine();
            Console.WriteLine(e.Message);
        }
    }

    private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
    {
        Console.WriteLine("Resolving...");
        return typeof(MyType).Assembly;
    }
}
Public Class MyType

    Public Sub New()
        Console.WriteLine()
        Console.WriteLine("MyType instantiated!")
    End Sub 'New

End Class 'MyType

Class Test

    Public Shared Sub Main()
        Dim currentDomain As AppDomain = AppDomain.CurrentDomain

        ' This call will fail to create an instance of MyType since the
        ' assembly resolver is not set
        InstantiateMyTypeFail(currentDomain)

        AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler

        ' This call will succeed in creating an instance of MyType since the
        ' assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain)

        ' This call will succeed in creating an instance of MyType since the
        ' assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain)
    End Sub 'Main

    Private Shared Sub InstantiateMyTypeFail(domain As AppDomain)
        ' Calling InstantiateMyType will always fail since the assembly info
        ' given to CreateInstance is invalid.
        Try
            ' You must supply a valid fully qualified assembly name here.
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
        Catch e As Exception
            Console.WriteLine()
            Console.WriteLine(e.Message)
        End Try
    End Sub 'InstantiateMyType

    Private Shared Sub InstantiateMyTypeSucceed(domain As AppDomain)
        Try
            Dim asmname As String = Assembly.GetCallingAssembly().FullName
            domain.CreateInstance(asmname, "MyType")
        Catch e As Exception
            Console.WriteLine()
            Console.WriteLine(e.Message)
        End Try
    End Sub

    Private Shared Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As Assembly
        Console.WriteLine("Resolving...")
        Return GetType(MyType).Assembly
    End Function 'MyResolveEventHandler

End Class 'Test

注釈

役割です、ResolveEventHandlerで指定されているアセンブリを返すには、このイベントのResolveEventArgs.Nameプロパティ、またはアセンブリが認識されない場合は null を返します。It is the responsibility of the ResolveEventHandler for this event to return the assembly that is specified by the ResolveEventArgs.Name property, or to return null if the assembly is not recognized. アセンブリは実行コンテキストに読み込まれる必要があります。リフレクション専用コンテキストに読み込まれる、負荷が発生します。 このイベントを発生させたは失敗します。The assembly must be loaded into an execution context; if it is loaded into the reflection-only context, the load that caused this event to be raised fails.

このイベントの使用に関するガイダンスについては、次を参照してください。アセンブリ読み込みの解決します。For guidance on the use of this event, see Resolving Assembly Loads.

以降では、 .NET Framework 4.NET Framework 4ResolveEventArgs.RequestingAssemblyプロパティは、解決できなかったアセンブリの読み込みを要求したアセンブリを返します。Beginning with the .NET Framework 4.NET Framework 4, the ResolveEventArgs.RequestingAssembly property returns the assembly that requested the assembly load that could not be resolved. たとえば、ローダーが、プローブ パスに要求元のアセンブリとその依存関係はないため、要求元のアセンブリの依存関係を読み込めませんしない場合もあります。For example, the loader might be unable to load a dependency of the requesting assembly because the requesting assembly and its dependency are not in the probing path. 依存関係の 1 つ以上のバージョンが利用可能な場合は、要求元のアセンブリの id を知ることが、依存関係を検出するか、適切なバージョンを識別するときに便利な可能性がありますに。Knowing the identity of the requesting assembly might be useful in locating the dependency or in identifying the correct version, if more than one version of the dependency is available. 詳細については、「 ResolveEventArgs.RequestingAssembly 」を参照してください。For more information, see ResolveEventArgs.RequestingAssembly.

重要

以降では、 .NET Framework 4.NET Framework 4ResolveEventHandlerリソース アセンブリを含め、すべてのアセンブリのイベントが発生します。Beginning with the .NET Framework 4.NET Framework 4, the ResolveEventHandler event is raised for all assemblies, including resource assemblies. 以前のバージョンでは、リソース アセンブリに対して、イベントが生成されません。In earlier versions, the event was not raised for resource assemblies. オペレーティング システムがローカライズされている場合、ハンドラーが複数回を呼び出すことがあります。 各カルチャ フォールバック チェーン内に 1 回です。If the operating system is localized, the handler might be called multiple times: once for each culture in the fallback chain.

このイベントで、ResolveEventArgs.Nameプロパティは、ポリシーが適用される前に、アセンブリ名を返します。For this event, the ResolveEventArgs.Name property returns the assembly name before policy is applied.

重要

イベント ハンドラーがない値を返すまでに、イベント ハンドラーが順序で呼び出されます場合、このイベントの 1 つ以上のイベント ハンドラーが登録されている、nullします。If more than one event handler is registered for this event, the event handlers are called in order until an event handler returns a value that isn't null. 後続のイベント ハンドラーは無視されます。Subsequent event handlers are ignored.

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。For more information about handling events, see Handling and Raising Events.

セキュリティ

SecurityCriticalAttribute
直前の呼び出し元に対する完全な信頼が必要です。Requires full trust for the immediate caller. このメンバーは、部分的に信頼されたまたは透過的なコードで使用することはできません。This member cannot be used by partially trusted or transparent code.

適用対象

こちらもご覧ください