Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

The dynamic type is part of System.Runtime.CompilerServices.DynamicAttribute, however, a reference to said DLL is not present. 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. Not under the Assemblies tab, nothing under Assemblies -> Framework or Extensions.

I'm just trying to grasp how this actually works, and how come I can't find any dependencies?

like image 240
Detilium Avatar asked May 02 '17 18:05

Detilium


1 Answers

Right-click the Dependencies node of your project > Manage Nuget Packages > Browse and type "dynamic" in the search box. Pick System.Dynamic.Runtime from the top of the list, now you can use dynamic in your source code.


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.

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. 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.

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.

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. 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.

On the other hand, something completely different is taking place when you declare the auto property of the class as 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.

like image 99
Eugene Komisarenko Avatar answered Sep 23 '22 06:09

Eugene Komisarenko