Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Utilisation de l’événement ‘AppDomain.AssemblyResolve’

Charger,une,Assemblée,avec,un,nom
{filelink=17564}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
 
public class MyType
{
    public MyType()
    {
        Console.WriteLine();
        Console.WriteLine("MyType instantiated!");
    }
}
 
class Test
{
 
    private static void InstantiateMyTypeFail(AppDomain domain)
    {
        try
        {
            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 static void Main()
    {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            InstantiateMyTypeFail(currentDomain);
            currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
            InstantiateMyTypeFail(currentDomain);
            InstantiateMyTypeSucceed(currentDomain);
    }
}

No comments yet.

Leave a Comment