英語で読む

次の方法で共有


AppDomain.TypeResolve イベント

定義

型の解決が失敗したときに発生します。

C#
public event ResolveEventHandler? TypeResolve;
C#
public event ResolveEventHandler TypeResolve;
C#
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public event ResolveEventHandler TypeResolve;

イベントの種類

実装

属性

次の例では、 イベントを TypeResolve 示します。

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。 完全修飾アセンブリ名を取得する方法については、「アセンブリ 名」を参照してください。

C#
using System;
using System.Reflection;
using System.Reflection.Emit;

class Test
{
    // For this code example, the following information needs to be
    // available to both Main and the HandleTypeResolve event
    // handler:
    private static AssemblyBuilder ab;
    private static string moduleName;

    public static void Main()
    {
        AppDomain currDom = AppDomain.CurrentDomain;

        // Create a dynamic assembly with one module, to be saved to
        // disk (AssemblyBuilderAccess.Save).
        //
        AssemblyName aName = new AssemblyName();
        aName.Name = "Transient";
        moduleName = aName.Name + ".dll";
        ab = currDom.DefineDynamicAssembly(aName,
            AssemblyBuilderAccess.Save);
        ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, moduleName);

        // The dynamic assembly has just one dummy type, to demonstrate
        // type resolution.
        TypeBuilder tb = mb.DefineType("Example");
        tb.CreateType();

        // First, try to load the type without saving the dynamic
        // assembly and without hooking up the TypeResolve event. The
        // type cannot be loaded.
        try
        {
            Type temp = Type.GetType("Example", true);
            Console.WriteLine("Loaded type {0}.", temp);
        }
        catch (TypeLoadException)
        {
            Console.WriteLine("Loader could not resolve the type.");
        }

        // Hook up the TypeResolve event.
        //
        currDom.TypeResolve +=
            new ResolveEventHandler(HandleTypeResolve);

        // Now try to load the type again. The TypeResolve event is
        // raised, the dynamic assembly is saved, and the dummy type is
        // loaded successfully. Display it to the console, and create
        // an instance.
        Type t = Type.GetType("Example", true);
        Console.WriteLine("Loaded type \"{0}\".", t);
        Object o = Activator.CreateInstance(t);
    }

    static Assembly HandleTypeResolve(object sender, ResolveEventArgs args)
    {
        Console.WriteLine("TypeResolve event handler.");

        // Save the dynamic assembly, and then load it using its
        // display name. Return the loaded assembly.
        //
        ab.Save(moduleName);
        return Assembly.Load(ab.FullName);
    }
}

/* This code example produces the following output:

Loader could not resolve the type.
TypeResolve event handler.
Loaded type "Example".
 */

注釈

このイベントは TypeResolve 、共通言語ランタイムが要求された型を作成できるアセンブリを特定できない場合に発生します。 これは、型が動的アセンブリで定義されている場合、または型が動的アセンブリで定義されていないが、ランタイムが型が定義されているアセンブリがわからない場合に発生する可能性があります。 後者の状況は、アセンブリ名で修飾されていない型名で が呼び出された場合 Type.GetType に発生する可能性があります。

このイベントの は ResolveEventHandler 、型の検索と作成を試みることができます。

ただし、特定のアセンブリで TypeResolve 型を見つけることができないことをランタイムが認識している場合、イベントは発生しません。 たとえば、静的アセンブリに型が見つからない場合、ランタイムは型を静的アセンブリに動的に追加できないことを認識しているため、このイベントは発生しません。

.NET Framework 4 以降では、 プロパティには型ResolveEventArgs.RequestingAssemblyを要求したアセンブリが含まれています。 詳細については、「ResolveEventArgs.RequestingAssembly」を参照してください。

このイベントのイベント ハンドラーを登録するには、必要なアクセス許可が必要です。または SecurityException がスローされます。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください