简体   繁体   English

尝试将其引用到其他项目中时出现dnlib库问题

[英]dnlib library issue when trying to reference it into other project

Using VisualStudio 2013 I've successfuly compiled the latest version of dnlib which is part of the ConfuserEx and de4dot projects, the dnlib project by default targets .NET Framework 2.0 so it should be compatible to reference it in other projects that targets a greater version but when I reference the resulting dnlib.dll in a new project that targets a .NET framework greater than 3.0 the project can't compile. 使用Visual Studio 2013,我已经成功编译了conflibsde4dot项目的一部分dnlib的最新版本,默认情况下dnlib项目以.NET Framework 2.0为目标,因此应该兼容于其他以更高版本为目标的项目中引用它。当我在针对大于3.0的.NET框架的项目中引用生成的dnlib.dll时 ,该项目无法编译。

There is any compiling error information just a VisualStudio messagebox that says: 仅VisualStudio消息框显示以下任何编译错误信息:

There were build errors. 出现构建错误。 Would you like to continue and run the last successful build? 您要继续并运行上一次成功的构建吗?

I've tried the dnlib.dll in a (new, empty) WinForms project using both the debug and the release version of dnlib.dll , I've tried targeting FW 3.5, 4.0, 4.5 and 4.5.1 but the project has been compiled successfuly only targeting FW 2.0 and 3.0. 我已经在dnlib.dll的调试版和发行版中尝试了(新的,空的)WinForms项目中的dnlib.dll ,我尝试了针对FW 3.5、4.0、4.5和4.5.1的目标,但该项目已经仅针对FW 2.0和3.0成功编译。

By the other hand, I'm totally able to navigate through the library members and instance the classes inside and all that, but no way to compile a project with that referenced dnlib.dll . 另一方面,我完全能够浏览库成员并实例化其中的类以及所有其他类,但是无法用引用的dnlib.dll编译项目。

I think that if the default FW targeting in the dnlib project is 2.0 should be for a good reason 'cause it touchs external assemblies and that, so I'm not sure to try to solve this issue by increasing the FW targeting in the dnlib project, but anyways I've tried to increase it to 4.0 to see what hapens and I get a lot of compiler errors about type defs in mscorlib.dll . 我认为,如果dnlib项目中的默认FW目标为2.0,应该是有充分的理由,因为它涉及外部程序集,因此,我不确定通过增加dnlib项目中的FW目标来解决此问题。 ,但是无论如何,我都尝试将其增加到4.0,以了解发生了什么情况,并且我在mscorlib.dll中遇到了很多有关类型defs的编译器错误。

I'm missing something? 我想念什么吗?

How I can solve this problem to be able to compile a project that targets FW4.0 with the dnlib.dll that targets FW2.0 referenced? 如何解决此问题,以便能够使用针对FW2.0的dnlib.dll编译针对FW4.0的项目?

Ok, fiddling around, I could get some Warnings but not errors. 好吧,摆弄我,我可以得到一些Warnings但没有错误。 As per MSDN, ExtensionAttribute came into being with Net 3.0 and HandleProcessCorruptedStateExceptionsAttribute with NET 4.0. 根据MSDN, ExtensionAttribute随Net 3.0一起出现,而HandleProcessCorruptedStateExceptionsAttribute与NET 4.0一起出现。 So, in order to make the NET 2.0 code compatible with 4.0 projects, it provides the missing Attributes. 因此,为了使NET 2.0代码与4.0项目兼容,它提供了缺少的属性。 The warnings are just that they are multiply defined, and since they are just Attributes, I don't think it matters. 警告只是它们被多重定义,并且由于它们只是属性,所以我认为这并不重要。

Rather than comment them out, there are 2 easy ways to get rid of the Warnings. 除了注释掉它们之外,还有两种简单的方法来摆脱警告。

Method 1 (better, I think): Create solutions for Net 2.0 and 4.0 builds. 方法1(我认为更好):为Net 2.0和4.0构建创建解决方案。

Open the basic Net 2.0 solution. 打开基本的Net 2.0解决方案。 Select the Solution (dnlib) in Solution Explorer. 在解决方案资源管理器中选择解决方案(dnlib)。 On the File menu pick Save dnlib.sln As and use dnlib20.sln as the file name. 在“文件”菜单上,选择“ 将dnlib.sln另存为”,然后使用dnlib20.sln作为文件名。 This is your NET 2.0 FrameWork Solution. 这是您的NET 2.0 FrameWork解决方案。

Do Save As again, this time to dblib40.sln . 再次将另存为 ,这一次是dblib40.sln This will be your Net 4.0 Framwwork solution in a moment. 稍后,这将是您的Net 4.0 Framwwork解决方案。 Switch both the console test project and dnlib library projects to Net 4.0 Target platform. 将控制台测试项目和dnlib库项目都切换到Net 4.0 Target平台。 Then, in Solution Explorer, exclude the last 2 files from the solution: HandleProcessCorruptedStateExceptionsAttribute.cs and ExtensionAttribute.cs . 然后,在解决方案资源管理器中,从解决方案中排除最后两个文件: HandleProcessCorruptedStateExceptionsAttribute.csExtensionAttribute.cs Save it, clean and build, and you should be good. 保存,清理和构建,您应该会很好。

You don't need these 2 files because NET has them defined (which is the warning); 您不需要这2个文件,因为NET已经定义了它们(这是警告)。 they are only meant for 2.0 projects/solutions. 它们仅适用于2.0项目/解决方案。

Note: You can also set it up so that the solutions compile to their own folders, so you dont get them confused. 注意:您也可以对其进行设置,以使解决方案可以编译到它们自己的文件夹中,以免混淆。 On the Build Tab, next output add a folder( ..\\Debug\\bin\\Net20 and ..\\Debug\\bin\\Net40 ). 在“构建”选项卡上,下一个输出添加一个文件夹( ..\\Debug\\bin\\Net20..\\Debug\\bin\\Net40 )。 You'll have to change/update for both Debug and Release versions. 您必须同时更改/更新Debug和Release版本。

Method 2: Define some conditional compiler constants. 方法2:定义一些条件编译器常量。

Create dblib20.sln and dblib40.sln solutions as above (unless you dont want to even use 2.0). 如上所述创建dblib20.slndblib40.sln解决方案(除非您甚至不想使用2.0)。 Be sure to set the Framework to NET 4 in the 4.0 solution. 确保在4.0解决方案中将Framework设置为NET 4。

In the Net 2.0 dnlib project file, go to Project Properties --> Build --> General add the conditional compile symbol NET20 . 在Net 2.0 dnlib 项目文件中,转到项目属性->生成->常规,添加条件编译符号NET20 Now, in HandleProcessCorruptedStateExceptionsAttribute.cs wrap the code there in a #if : 现在,在HandleProcessCorruptedStateExceptionsAttribute.cs将代码包装在#if

#if NET20

using System;
#pragma warning disable 1591    // XML doc warning

namespace System.Runtime.ExceptionServices {
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    sealed class HandleProcessCorruptedStateExceptionsAttribute : Attribute {
    }
}

#endif

In ExtensionAttribute.cs , do the same thing. ExtensionAttribute.cs ,执行相同的操作。 Then Clean and Rebuild, and all should be well. 然后清理并重建,一切都应该很好。 Since the symbol is only defined in the NET20 solution, those classes will only be compiled when you use/open the Net 2.0 solution. 由于该符号仅在NET20解决方案中定义,因此仅在使用/打开Net 2.0解决方案时才编译这些类。 If you download another update, you will have to redo this unless they provide a NET 4.0 solution file. 如果您下载另一个更新,则除非它们提供NET 4.0解决方案文件,否则您将不得不重做此更新。

AFAIK, there still are not any built in FrameWork constants. AFAIK,仍然没有任何内置的FrameWork常量。

Finally, when those warnings are cleared away, there are 5 compare warnings, but from the looks of it, those can be ignored (the code seems to be doing what it wants). 最后,当这些警告被清除时,有5个比较警告,但是从其外观看,那些警告可以忽略(代码似乎在执行其所需的操作)。

This worked for me: 这为我工作:

Change dnlib project's target version to .NET 4.0 Framework Client Profile. 将dnlib项目的目标版本更改为.NET 4.0 Framework Client Profile。 Change the Example project target version to .NET 4.0 Framework Client Profile. 将示例项目目标版本更改为.NET 4.0 Framework Client Profile。 Remove/comment out ExtensionAttribute in ExtensionAttribute.cs in dnlib Remove/comment out HandleProcessCorruptedStateExceptionsAttribute in dnlib I then compiled and got no errors. 删除/注释掉dnlib的ExtensionAttribute.cs中的ExtensionAttribute。删除/注释掉dnlib的HandleProcessCorruptedStateExceptionsAttribute,然后我进行了编译,没有出现错误。

I used Visual Studio 2012. 我使用了Visual Studio 2012。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试引用项目媒体文件夹时出现路径问题 - Issue with path when trying to reference project media folder 引用 .net 核心类库到项目的问题 - Issue with reference .net core class library to project WCF服务库项目找不到对其他项目的引用 - WCF service library project can't find reference to other project C# - MVC - 参考 class 库项目到主项目的问题 - C# - MVC - Issue with reference a class library project to main project 当项目需要引用其他dll时 - when project needs reference to other dlls 尝试将Web服务引用添加到项目时的HTTP状态400 - HTTP status 400 when trying to add web service reference to a project 将.NET Core类库引用添加到UWP应用时,显示“无法添加对项目的引用…”。 - When adding .NET Core class library reference to UWP app, it says “Unable to add a reference to project…” 试图用monogame项目和类库创建解决方案,但不允许我在它们之间添加引用 - Trying to create a solution with a monogame project and a class library, and it won't let me add a reference between them 合并两个方法的IL时dnlib.DotNet.Writer.ModuleWriterException - dnlib.DotNet.Writer.ModuleWriterException when merging IL of two methods 发布 Blazor 项目,库项目参考 Azure - Publish Blazor project with library project reference to Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM