AppDomain.AssemblyResolve Event : AppDomain « Reflection « C# / C Sharp






AppDomain.AssemblyResolve Event

  

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
{
    public static void Main()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        InstantiateMyTypeFail(currentDomain);
        currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
        InstantiateMyTypeFail(currentDomain);
        InstantiateMyTypeSucceed(currentDomain);
    }
    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;
    }
}

   
    
  








Related examples in the same category

1.Get AppDomainSetup
2.Assembly loaded event
3.Gets the base directory that the assembly resolver uses to probe for assemblies.
4.Resets the path that specifies the location of private assemblies to the empty string ("").
5.Creates a new instance of a specified COM type.
6.Creates a new instance of the specified type.
7.Creates a new instance of the specified type defined in the specified assembly file.
8.Gets the current application domain for the current Thread.
9.Defines a dynamic assembly using the specified name, access mode, and storage directory.
10.Gets the friendly name of this application domain.
11.Gets the assemblies that have been loaded into the execution context of this application domain.
12.Loads an Assembly given its AssemblyName.
13.Returns the assemblies that have been loaded into the reflection-only context of the application domain.
14.Establishes security policy level for application domain.
15.Assigns the specified value to the specified application domain property.
16.Get App Setting String