动态加载后如何使用依赖DLL方法?

时间:2012-03-12 12:03:56

标签: c# asp.net .net dll appdomain

我可以动态加载Dlls。我想在加载程序集后使用GetSum方法。

namespace ClassLibrary2
{
    public class Class1
    {
        public Class1()
        {

        }

        public int GetSum(int a , int b)
        {
            try
            {
                ClassLibrary1.Class1 ctx = new ClassLibrary1.Class1();
                return ctx.Sum(a, b);
            }
            catch
            {
                return -1;
            }

        }
    }
}

enter image description here

我无法使用GetSum方法。

public void LoadAndCall(List<byte[]> binaryList)
{
    Assembly loadedAssembly=null;

    Assembly[] assBefore = AppDomain.CurrentDomain.GetAssemblies();
    foreach (byte[] binary in binaryList)
    {
        loadedAssembly = AppDomain.CurrentDomain.Load(binary);
        //foreach (var assemblyName in loadedAssembly.GetReferencedAssemblies())
        //    AppDomain.CurrentDomain.Load(assemblyName);
    }
    Assembly[] assAfter = AppDomain.CurrentDomain.GetAssemblies();
 //   AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    object[] tt = { 3, 6 };
    Type type = loadedAssembly.GetType("ClassLibrary2.Class1");
    object loaded = loadedAssembly.CreateInstance("ClassLibrary2.Class1", true, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, null, new object[] { }, null, null);
   // object obj = Activator.CreateInstance(type);



    ObjectCreateMethod inv = new ObjectCreateMethod(type); //Specify Type
    Object obj = inv.CreateInstance();

    MethodInfo minfo = type.GetMethod("GetSum", BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance);
    int x = (int)minfo.Invoke(obj, new Object[] { 3, 6 });
    Console.WriteLine(x);
}

如何使用GetSum方法? classlib2依赖于classlib1库。它们是动态加载Appdomain。

0 个答案:

没有答案
相关问题