[英]Assembly has been tampered
I am trying to embed a third party library into my DLL. 我试图将第三方库嵌入到我的DLL中。 The DLL has been referenced and set not to copy local as well as added to the resources.
DLL已被引用,并且设置为不复制本地文件,也不添加到资源中。 I then do the following to load the DLL at during runtime.
然后,我执行以下操作以在运行时加载DLL。
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name + ".dll";
string resource = Array.Find(Assembly.GetExecutingAssembly().GetManifestResourceNames(), element => element.EndsWith(resourceName));
Assembly assembly;
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
assembly = Assembly.Load(assemblyData);
stream.Close();
}
return assembly;
};
The assembly is returned and I can step further into the code but when instantiating a new object from the DLL's constructor I receive a TypeInitilizationException with an inner message saying "Assembly has been tampered". 返回了程序集,我可以进一步进入代码,但是当从DLL的构造函数实例化新对象时,我收到TypeInitilizationException并带有内部消息,提示“程序集已被篡改”。
Is there anyway around this or is my method of loading the DLL wrong? 无论如何还是我加载DLL的方法错误?
EDIT 编辑
instance = new iConfServerDotNet();
also tried 也尝试过
System.Reflection.ConstructorInfo constructorInfo = typeof(iConfServerDotNet).GetConstructor(new Type[] { });
if (constructorInfo.DeclaringType.Name == "iConfServerDotNet")
{
object o = constructorInfo.Invoke(new Object[] { }) as UserControl;
}
EDIT 2 编辑2
The new code to get the type of class... and call it's contstructor. 获取类类型的新代码...并称其为构造函数。 This still causes the same exception.
这仍然会导致相同的异常。
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name + ".dll";
string resource = Array.Find(Assembly.GetExecutingAssembly().GetManifestResourceNames(), element => element.EndsWith(resourceName));
Assembly assembly;
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
assembly = Assembly.Load(assemblyData);
stream.Close();
}
Type type = assembly.GetModule(resourceName).GetType("iConfServer.NET.iConfServerDotNet");
//object instance = type.GetConstructors()[0].Invoke(null);
object instance = Activator.CreateInstance(type);
return assembly;
};
Exception occurs at Activator.CreatInstance(type); Activator.CreatInstance(type);发生异常;
Assuming these statements where correct (note that these where posted by the original posted in the comments underneath the question): 假设这些陈述正确无误(请注意,这些陈述是由问题下方评论中的原始陈述所发表的):
The problem probably lies in your way of invoking the constructor. 问题可能出在您调用构造函数的方式上。 I've created a simple testing library to show you how you can do this properly:
我创建了一个简单的测试库,向您展示如何正确执行此操作:
First, the testing library: 一,测试库:
namespace TestLibrary
{
public class Main
{
public string GetString()
{
return "Hey, I'm working well!";
}
}
}
I've compiled this to an dynamic link library (.DLL) and added it as reference to an console application project (also, just for testing). 我已经将其编译为动态链接库(.DLL),并将其添加为对控制台应用程序项目的引用 (也用于测试)。
As a note, I've changed the Build Action
property of the resource (you can open the property window by right clicking on the resource in the Solution Explorer
and clicking Properties
) to Embedded Resource
. 请注意,我将资源的
Build Action
属性(可以通过在Solution Explorer
右键单击Solution Explorer
并单击Properties
来打开属性窗口)更改为Embedded Resource
。
This is your code (I just changed the first part as I didn't need it, feel free to change it back) to load the library into our running application: 这是您的代码(由于不需要,我只是更改了第一部分,可以随时将其改回)以将库加载到我们正在运行的应用程序中:
string resourceName = "TestLibrary.dll";
string resource = Array.Find(Assembly.GetExecutingAssembly().GetManifestResourceNames(), element => element.EndsWith(resourceName));
Assembly assembly;
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
assembly = Assembly.Load(assemblyData);
stream.Close();
}
Now, to create a new instance of the class we need to find the type of the class first: 现在,要创建该类的新实例,我们需要首先找到该类的类型:
Type _typeOfClass = assembly.GetModule("TestLibrary.dll").GetType("TestLibrary.Main");
_typeOfClass
will now contain the Type that leads us to The Main
class inside the library. _typeOfClass
现在将包含Type,该Type会将我们引到库中的Main
类。 Next we are going to create a new instance of the class and we are going to invoke the GetString
method to see if it actually worked: 接下来,我们将创建该类的新实例,并将调用
GetString
方法以查看其是否真正起作用:
// Create an instance of the class invoking the (only) constructor.
object _instance = _typeOfClass.GetConstructors()[0].Invoke(null);
// Call the method on the instance we just instantiated
object result = _typeOfClass.GetMethod("GetString").Invoke(_instance, null);
Console.WriteLine(result);
The Console will now show: Hey, I'm Working well! 控制台现在将显示: 嘿,我工作得很好! .
。
If you have multiple constructors and you just want to get the default constructor (no parameters) you can use object _instance = Activator.CreateInstance(_typeOfClass);
如果您有多个构造函数,而只想获取默认的构造函数(无参数),则可以使用
object _instance = Activator.CreateInstance(_typeOfClass);
too. 太。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.