【发布时间】:2012-03-10 08:13:38
【问题描述】:
我想加载这个类库:
namespace ClassLibrary1
{
public class Class1
{
public Class1()
{
}
public static int Sum(int a, int b)
{
return a + b;
}
}
}
我有一个 wcf 服务,它返回给我一个 byte[] 数组 (ClassLibrary1) 我无法加载此程序集
static void Main(string[] args)
{
FileTransferService.ApplicationHostServiceClient client = new FileTransferService.ApplicationHostServiceClient();
FileTransferService.AssemblyPackage[] asmbs = client.GetFile();
//var newDomain = AppDomain.CreateDomain("FooBar", null, null);
foreach (FileTransferService.AssemblyPackage item in asmbs)
{
byte[] mybuffer = item.Buffer;
new AssemblyLoader().LoadAndCall(mybuffer);
}
}
public class AssemblyLoader : MarshalByRefObject
{
public void LoadAndCall(byte[] binary)
{
Assembly loadedAssembly = AppDomain.CurrentDomain.Load(binary);
object[] tt = { 3, 6 };
Type typ = loadedAssembly.GetType("ClassLibrary1.Class1");
MethodInfo minfo = typ.GetMethod("Sum", BindingFlags.Public);
int x = (int)minfo.Invoke(null, tt);
Console.WriteLine(x);
}
}
在这个方法中返回错误:Assembly loadedAssembly = AppDomain.CurrentDomain.Load(binary);
错误:
BADIMAGEFORMAT EXCEPTION
Could not load file or assembly '4096 bytes loaded from Client2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
例外:
Bad IL format
我在谷歌上搜索过这种错误,但没有确切的解决方案。我想使用 AppDomain 加载我的程序集。
【问题讨论】:
-
4096 字节听起来有点像 WCF 限制,可能与此类似; fudofuad.wordpress.com/2010/04/26/…
-
我们可以看到服务器端代码读取/发送程序集吗?
标签: c# .net reflection appdomain