简体   繁体   English

在NETStandard类库中添加引用

[英]Adding references in NETStandard class library

I'm trying to get my head around the NETStandard class libraries, for a potential NuGet package I'm working on. 我正试图绕过NETStandard类库,找到我正在研究的潜在NuGet包。 This is the very first time I'm playing around with NuGet and NETStandard packages however, so I'm a bit lost on some points, one of these being referencing system libraries. 这是我第一次使用NuGet和NETStandard软件包,所以我在某些方面有点迷失,其中一个是引用系统库。

The dynamic type is part of System.Runtime.CompilerServices.DynamicAttribute , however, a reference to said DLL is not present. dynamic类型是System.Runtime.CompilerServices.DynamicAttribute一部分,但是,不存在对所述DLL的引用。 I though that I'd just go ahead and add it, but I can't seem to do so. 我虽然我会继续添加它,但我似乎无法这样做。 I suppose this doesn't work like a framework class library? 我想这不像框架类库那样工作?

If I right-click on my Dependencies folder in my solution explorer, and click on Add Reference... , I can't seem to find ANY dependencies that I can use. 如果我在解决方案资源管理器中右键单击我的Dependencies文件夹,然后单击Add Reference... ,我似乎无法找到可以使用的任何依赖项。 Not under the Assemblies tab, nothing under Assemblies -> Framework or Extensions . 不在Assemblies选项卡下, Assemblies - > FrameworkExtensions下没有任何内容。

I'm just trying to grasp how this actually works, and how come I can't find any dependencies? 我只是想了解这实际上是如何工作的,为什么我找不到任何依赖?

Right-click the Dependencies node of your project > Manage Nuget Packages > Browse and type "dynamic" in the search box. 右键单击项目的“依赖项”节点>“管理Nuget包”>“浏览”,然后在搜索框中键入“dynamic”。 Pick System.Dynamic.Runtime from the top of the list, now you can use dynamic in your source code. 从列表顶部选择System.Dynamic.Runtime ,现在您可以在源代码中使用dynamic


If you were like me interested in the reason of why you can use dynamic keyword without referencing DLR libraries in the code of the method, but cannot in the declarations of the properties, keep reading. 如果您对我感兴趣的原因是为什么您可以使用dynamic关键字而不在方法的代码中引用DLR库,但不能在属性的声明中继续阅读。

Have a look at the following lines of C# code below which are compiling with no issue using NETStandard.Library without referencing System.Dynamic.Runtime . 看看下面的C#代码行,它们在没有引用System.Dynamic.Runtime情况下使用NETStandard.Library进行编译时没有任何问题。 The main difference between Line 1 and Line 3 in the C# code below is the usage of the keyword dynamic vs var or dynamic vs static typing. 下面C#代码中第1行和第3行之间的主要区别是关键字dynamic vs var或dynamic vs static typing的用法。

Line 1: dynamic a = new {a = 1, b = 2};
Line 2: a = new Class1();
Line 3: var b = new { a = 1, b = 2 };

And here is the simplified IL of the same three lines for your reference. 以下是相同三行的简化IL供您参考。

Line 1: newobj     instance void class '<>f__AnonymousType0`2'<int32,int32>::.ctor(!0,!1)
Line 2: newobj     instance void ClassLibrary2.Class1::.ctor()
Line 3: newobj     instance void class '<>f__AnonymousType0`2'<int32,int32>::.ctor(!0,!1)

If you compare Line 1 and Line 3 of the resulting IL code you will find no difference at all. 如果比较生成的IL代码的第1行和第3行,您将发现没有任何区别。 The compiler can infer the type of the variable from the initialization code as well as enable re-initialization of the same variable for different types. 编译器可以从初始化代码推断变量类型,并为不同类型启用相同变量的重新初始化。 No dependency on DLR required in this scenario. 此方案中不需要依赖DLR。

On the other hand, something completely different is taking place when you declare the auto property of the class as dynamic . 另一方面,当您将类的auto属性声明为dynamic时,会发生完全不同的事情。

public class Class1 { public dynamic Test { get; set; } }

The IL code of the auto-property reveals dynamic property being converted to a private backing field of the type object as well as the code of the initialization, getter, and setter which is heavily relying on the System.Runtime.CompilerServices.DynamicAttribute from System.Dynamic.Runtime bringing in the dependency to DLR in this case. 自动属性的IL代码揭示了dynamic属性被转换为类型object的私有后备字段以及初始化,getter和setter的代码,它严重依赖于System.Runtime.CompilerServices.DynamicAttribute来自System.Dynamic.Runtime在这种情况下引入了对DLR的依赖。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM