简体   繁体   English

我可以在 .NET Standard 类库中使用 dynamic 吗?

[英]Can I use dynamic in a .NET Standard class library?

I'm having a hard time trying to migrate from a regular Windows desktop development to ASP.NET Core MVC.我很难从常规的 Windows 桌面开发迁移到 ASP.NET Core MVC。 One issue I'm coming across is to create my solution.我遇到的一个问题是创建我的解决方案。 I would like to remove everything that is not UI related from the default project that ships with VS 2015 and put into a separate project.我想从 VS 2015 附带的默认项目中删除与 UI 无关的所有内容,并将其放入单独的项目中。

I noticed that ASP MVC Core references to .NETCoreApp and the Class Library project references to .NETStandard .我注意到到ASP MVC核心引用.NETCoreApp和类库项目引用.NETStandard

My problem is that I need to use the dynamic keyword in the class library and it does not support it.我的问题是我需要在类库中使用dynamic关键字并且它不支持它。 The MVC project supports without any problem. MVC 项目支持没有任何问题。 I guess it's because of the different .NET versions.我想这是因为不同的 .NET 版本。

  1. What is the difference between NETStandard and NETCoreApp? NETStandard 和 NETCoreApp 有什么区别?

  2. Can I create a class library that uses the same reference as the MVC project so I can use the dynamic keyword on it?我可以创建一个使用与 MVC 项目相同的引用的类库,以便我可以在其上使用 dynamic 关键字吗?

  3. Or should I stick with a one project solution with all domain, infrastructure, etc in the same place?或者我应该坚持使用一个项目解决方案,将所有域、基础设施等放在同一个地方?

Yes, it's possible to put non-UI code into a separate library.是的,可以将非 UI 代码放入单独的库中。 As you guessed, netcoreapp1.0 is for applications (console or web), and netstandard1.X is for class libraries.如您所料, netcoreapp1.0用于应用程序(控制台或 Web),而netstandard1.X用于类库。

A .NET Standard class library shouldn't have any problem with the dynamic keyword. .NET Standard 类库的dynamic关键字应该没有任何问题。 You do need to reference NETStandard.Library , though.不过,您确实需要引用NETStandard.Library Here's a barebones working library example:这是一个准系统工作库示例:

MyLibrary/project.json我的图书馆/project.json

{
   "description": "My awesome library",
   "dependencies": {
      "NETStandard.Library": "1.6.0"
   },
   "frameworks": {
      "netstandard1.3": { }
   }
}

Your ASP.NET Core web application can reference this library if it's part of the same solution like this:如果您的 ASP.NET Core Web 应用程序属于相同解决方案的一部分,则它可以引用此库,如下所示:

{
  (... other stuff)

  "dependencies": {
    "MyLibrary": {
      "target": "project"
    }
  }
}

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

相关问题 我可以使用.Net标准库中的PowerShell类在.Net Framework项目(exe)中使用吗 - Can I use PowerShell class in .Net Standard library to use in .Net Framework project (exe) 我可以在标准.Net应用程序中使用PCL库吗? - Can I use PCL library in standard .Net application? 如何将 .NET 标准库更改为 .NET 框架库? - How can I change a .NET standard library to a .NET framework library? .NET Standard 2.0 Class 库能否依赖于 .NET Framework 4.7.2? - Can .NET Standard 2.0 Class Library depend on .NET Framework 4.7.2? 我可以在类库中使用log4net吗? - Can i use log4net in class library? 如何在类库中使用log4net - How can I use log4net in Class Library 无法访问 .NET Standard 类库中的 HttpRuntime.Cache - Can't access HttpRuntime.Cache in .NET Standard class library 我不能在Portable Class Library中使用dynamic? - I cant use dynamic with Portable Class Library? 如何通过仅使用.NET Core SDK直接调用C#编译器来编译.NET Standard 2.0类库? - How can I compile a .NET Standard 2.0 class library by directly invoking the C# complier using only the .NET Core SDK? 如何为C#类库多目标以同时支持NET_STANDARD和NET_COREAPP? - How can I multi-target a C# Class Library to support both NET_STANDARD and NET_COREAPP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM