Я могу загружать 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;
}
}
}
}
Я не могу использовать метод 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.