简体   繁体   English

组装已被篡改

[英]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): 假设这些陈述正确无误(请注意,这些陈述是由问题下方评论中的原始陈述所发表的):

  1. "The type initializer for .. threw an exception." “ ..的类型初始值设定项引发了异常。” at iConfServer.NET.iConfServerDotNet..ctor()" 在iConfServer.NET.iConfServerDotNet..ctor()中”
  2. The assembly has appropriate information when debugging 调试时程序集具有适当的信息

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.

相关问题 无法加载文件或程序集:该程序集可能已被篡改,或者已被延迟签名但未使用正确的私钥进行完全签名 - Could not load file or assembly: The assembly may have been tampered with, or it was delay signed but not fully signed with the correct private key 无法加载文件或程序集“EntityFramework”或其依赖项之一。 组件可能已被篡改 - Could not load file or assembly 'EntityFramework' or one of its dependencies. The assembly may have been tampered with 检查组件是否已加载 - Check if assembly has been loaded 许可证执行系统已被篡改调用wkhtmltopdf.exe-Win Server 2008 - license enforcement system has been tampered with on calling to wkhtmltopdf.exe - win server 2008 有没有办法判断是否已使用优化参数编译C#程序集? - Is there a way to tell if a C# assembly has been compiled with the optimization parameter? 无法解析对程序集“PostSharp”的依赖性,因为它尚未预加载 - Cannot resolve dependency to assembly 'PostSharp' because it has not been preloaded 查找已重命名文件的进程的程序集名称 - Find the assembly name of a process who's file has been renamed 名为 account 的代理类型已由另一个程序集定义 - a proxy type with the name account has been defined by another assembly 使用通过ILMerge创建的库中的程序集 - Use assembly from a library which has been created through ILMerge 资源标识符“ App.xaml”已在Xamarin中的此程序集中使用[VS2017] - Resource identifier 'App.xaml' has already been used in this assembly in Xamarin [VS2017]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM