DLL 프로젝트에서 종속된 DLL들을 로드를 할때 프로그래밍적으로 종속된 DLL의 path를 변경해야될

경우가 있었다. 아래 코드는 프로그래밍적으로 종속된 DLL들의 path를 변경해준다



public void test()

{

AppDomain currentDomain = AppDomain.CurrentDomain;

currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

}


private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)

{

    //This handler is called only when the common language runtime tries to bind to the assembly and fails.


    //Retrieve the list of referenced assemblies in an array of AssemblyName.

    Assembly MyAssembly, objExecutingAssembly;

    string strTempAssmbPath = "";


    objExecutingAssembly = Assembly.GetExecutingAssembly();

    AssemblyName[] arrReferencedAssmbNames = objExecutingAssembly.GetReferencedAssemblies();


    //Loop through the array of referenced assembly names.

    foreach(AssemblyName strAssmbName in arrReferencedAssmbNames)

    {

        //Check for the assembly names that have raised the "AssemblyResolve" event.

        if(strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))

        {

            //Build the path of the assembly from where it has to be loaded.                

            strTempAssmbPath = "C:\\Myassemblies\\" + args.Name.Substring(0,args.Name.IndexOf(","))+".dll";

            break;

        }


    }


    //Load the assembly from the specified path.                    

    MyAssembly = Assembly.LoadFrom(strTempAssmbPath);                   


    //Return the loaded assembly.

    return MyAssembly;          

}


[참고자료]

http://stackoverflow.com/questions/1373100/how-to-add-folder-to-assembly-search-path-at-runtime-in-net


Posted by 모과이IT
,