.net核心类库调用.net框架类库 [英] .net core classlibrary calling .net framework class library

查看:34
本文介绍了.net核心类库调用.net框架类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找不到我的疑问的答案,希望有人能澄清.

Could not find an answer to my doubts and hopefully somebody can clarify.

我用

  • 1 个类库(.net 框架)
  • 1 个 .net 核心库

尝试引用任何一种方式,但我不能,它们不兼容,很好.

Tried to reference either way but I cant,they are not compatible,fine makes sense.

现在我的问题

我有一个实用程序类库(.net 框架),带有扩展、帮助程序等...现在由 winforms-wpf-asp.net mvc 4,5 应用程序使用的 .net core 事件在我看来,除非我将它移植到 .net core,否则我不能再使用这个库,然后我不能使用我的其他应用.

I have a utility class library(.net framework)with extensions,helpers etc... that is used by winforms-wpf-asp.net mvc 4,5 apps now with the event of .net core it looks to me that I cannot use this library anymore unless I port it to .net core,which then i cannot use with my other apps.

正确的方法是什么?

我是否错过了显而易见的东西?

Am i missing the obvious?

推荐答案

在普通 .NET 库和 Core 项目之间共享代码对我来说并不适合通过简单地使用共享项目,因为我无法从 Core 中引用它项目.

Sharing code between a normal .NET library and a Core project did not work for me via simply using a Shared project, because I could not reference it from the Core project.

不过,我可以通过一个小技巧让它发挥作用.

However, with a little trick I could make it work.

让我解释一下这个文件夹/文件结构:

Let me explain with this folder/file structure:

[ProjectName] // Root of Core project
    project.json
    [ProjectName].xproj

    Shared // Root of Shared project
        [ProjectName].Shared.projitems
        [ProjectName].Shared.shproj 
        // -- Source files here --

    Net // Root of .NET project
        [ProjectName].csproj
        Properties
            AssemblyInfo.cs // For both Core and .NET project
        // NO source files here

因此,您当然需要 3 个项目:一个 Core 项目、一个普通的 .NET 项目和一个共享项目.
Shared 项目包含所有源文件.
.NET 项目引用了共享项目,因此它也有这些文件.
Core 项目会看到 Shared 项目拥有的所有文件,因此它也拥有相同的文件.

So, you will need 3 projects, of course: a Core project, a normal .NET project, and a Shared project.
The Shared project has all the source files.
The .NET project references the Shared project, so it also has those files.
The Core project sees all the files the Shared project has, so it also has the same files.

就是这样.您现在可以拥有 .NET 和 Core 项目的通用源代码文件.

That's it. You now can have common source code files for the .NET and the Core project.

一些注意事项:

A few notes:

  • 永远不要将 .shroj.csproj 放在同一个文件夹中.对我来说,它在 VS (2015) 中完全关闭了智能感知.这些信息让我很痛苦...
  • 您可以使用#if -s 对常用代码进行微调
  • 您还可以在具有上述文件夹结构的 .NET 项目中使用 NuGet 2.
    • 请注意,如果您将 (NuGet 2) packages.config 放入 (NuGet 3) project.json 所在的同一文件夹中,则后者会完全覆盖前者.
    • Do NOT ever put the .shroj and the .csproj into the same folder. For me, it totally turned off intellisense in VS (2015). This information costed a lot of pain for me...
    • You can use #if -s to fine tune the common code
    • You can also use NuGet 2 in the .NET project with the above folder structure.
      • Note, that if you'd put the (NuGet 2) packages.config into the same folder where the (NuGet 3) project.json is located, the latter would totally overwrite the earlier.

      这篇关于.net核心类库调用.net框架类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆