Microsoft Reduce Customer Effort Center

Our team drives product feedback based on solid data, it drives proactive issue prevention and ultimately, drives improvements around products based on customer feedback.

C# - How to load DLL in separate domain and use its methods?

C# - How to load DLL in separate domain and use its methods?

  • Comments 1
  • Likes

Sometimes you may have a need to load a DLL in a separate domain to call its method at run-time. You can create a proxy class by referencing the following code snippet:

public class Loader : MarshalByRefObject
 {
     object CallInternal(string dll, string typename, string method, object[] parameters)
     {
         Assembly a = Assembly.LoadFile(dll);
         object o = a.CreateInstance(typename);
         Type t = o.GetType();
         MethodInfo m = t.GetMethod(method);
         return m.Invoke(o, parameters);
     }
     public static object Call(string dll, string typename, string method, params object[] parameters)
     {
         AppDomain dom = AppDomain.CreateDomain("MyNewDomain");
         Loader ld = (Loader)dom.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Loader).FullName);
         object result = ld.CallInternal(dll, typename, method, parameters);
         AppDomain.Unload(dom);
         return result;
     }
 }
Comments
  • I'm very thankful !!! It worked very well to me!!!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment