{"id":1578,"date":"2019-02-01T15:34:14","date_gmt":"2019-02-01T20:34:14","guid":{"rendered":"http:\/\/codinggorilla.domemtech.com\/?p=1578"},"modified":"2019-02-08T13:35:44","modified_gmt":"2019-02-08T18:35:44","slug":"what-the-hell-is-that","status":"publish","type":"post","link":"http:\/\/165.227.223.229\/index.php\/2019\/02\/01\/what-the-hell-is-that\/","title":{"rendered":"What the hell is that?"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"What the Hell is that? Carl from Caddyshack 1st public appearance\" width=\"640\" height=\"480\" src=\"https:\/\/www.youtube.com\/embed\/-l5tpY6SXMc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n<p>No ifs, ands, or buts, news about Net Standard may be old and stale, but one thing still applies: As Steve Martin would say, <em>&#8220;What the hell is <strong>that<\/strong>?&#8221;<\/em>\u00c2\u00a0Believe it or not, I started writing this entry years ago, a few months after Net Standard first came out. I had hoped that if I just write about <em><strong>it<\/strong><\/em>, I&#8217;d somehow stumble across what the hell <strong><em>it<\/em><\/strong> is. But, I didn&#8217;t get anywhere because most people don&#8217;t know what the hell they&#8217;re talking about. Fast forward to now.<\/p>\n<p><!--more--><\/p>\n<p>Due to my work on writing a C# compiler for the GPU called Campy, I&#8217;ve made another diversion into Net Standard to try to understand what is going on here. Unfortunately, the folks at Microsoft don&#8217;t help. <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\">In one place<\/a>, they say Net Standard is &#8220;a <strong><em>formal specification<\/em><\/strong> of .NET APIs that are intended to be available on all .NET implementations.&#8221; <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard#official-artifacts\">In another place<\/a>, MS says &#8220;The official specification is a set of .cs files that define the APIs that are part of the standard. The\u00c2\u00a0<a href=\"https:\/\/github.com\/dotnet\/standard\/tree\/master\/src\/netstandard\/ref\" data-linktype=\"external\">ref directory<\/a>\u00c2\u00a0in the\u00c2\u00a0<a href=\"https:\/\/github.com\/dotnet\/standard\" data-linktype=\"external\">dotnet\/standard repository\u00c2\u00a0<\/a>defines the .NET Standard APIs.&#8221; Another, a well-known developer and ex-professor, <a href=\"https:\/\/www.hanselman.com\/blog\/?date=2017-06-16\">wrote<\/a> &#8220;.NET Standard isn&#8217;t a runtime. <em><strong>It&#8217;s not something you can install.<\/strong><\/em> It&#8217;s not an &#8216;instance of .NET.'&#8221; If you go to the Net Standard FAQ, there&#8217;s a <a href=\"https:\/\/github.com\/dotnet\/standard\/blob\/master\/docs\/faq.md\">whole lot of stuff<\/a>, mostly not useful. And, you get a hint about what the hell it is if you try to sit back and watch a <a href=\"https:\/\/www.youtube.com\/watch?v=vg6nR7hS2lI&amp;list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY&amp;index=4\">long lecture from some guy<\/a>\u00c2\u00a0on type forwarding.<\/p>\n<p><!--more--><\/p>\n<p>These descriptions at best use imprecise English. The reality is that there is no spec, because as MS admits it&#8217;s really just C# code. End of game. When I was growing up many years ago, a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Specification_(technical_standard)\"><strong>specification<\/strong><\/a> was a document, not code. If my advisor 30 years ago saw a student handing in code for a spec, then he&#8217;d give out an F. Even in a first-year computer science class, you learn that while there may be some different types of specifications, an implementation isn&#8217;t one. Why? Specifications should describe <em>what something is supposed to do, what is is supposed to fulfill<\/em>. A bunch of C# files doesn&#8217;t indicate how it&#8217;s packaged up, what each type in the API does, what happens if the implementations (Net Core, Net Framework, Mono) differ. The document at\u00c2\u00a0 https:\/\/raw.githubusercontent.com\/dotnet\/standard\/master\/docs\/versions\/netstandard2.0_ref.md, nor the code at\u00c2\u00a0https:\/\/github.com\/dotnet\/standard\/tree\/master\/src\/netstandard\/ref, are <em>formal specifications<\/em>, nothing like\u00c2\u00a0what we see in a traditional sense, e.g., B [Abr 96], Vienna development method (VDM; [Luc 87]), etc. Some people say it&#8217;s just that .md file. But, that brings up the question: Is the .md file the spec, or the C# files? And, what happens if they differ?<\/p>\n<h3>Is it a specification?<\/h3>\n<p>Microsoft will keep insisting that C# code is a spec. But, there are no semantics for the API described, no description of what each method does, no <a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_by_contract\">pre-, post-conditions, nor invariants<\/a>. For all I know, a method like System.Console.WriteLine() could do anything, even compute the value of <span class=\"texhtml\">\u00cf\u20ac, yet would it still be\u00c2\u00a0a framework that conforms to Net Standard?<\/span>\u00c2\u00a0There could be a spec for Net Standard, but the C# ref files should never be called a specification.<\/p>\n<p>What is <em><strong>it<\/strong><\/em>? Net Standard is C# code, which employs some cleverness to generate an interchangeable assembly called netstandard.dll for each platform. Each netstandard.dll contains the types for a common subset of across the different platforms.<\/p>\n<h3>Type Forwarding<\/h3>\n<p>The crux of Net Standard is a compiler feature called\u00c2\u00a0<em>type forwarding<\/em>. Type forwarding allows one to set a link of a type in an &#8220;interface assembly&#8221; (or what MS calls a &#8220;reference assembly&#8221;&#8211;more details about the term below), to a type in an &#8220;implementation assembly&#8221;. Type forwarding uses the attribute <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.runtime.compilerservices.typeforwardedtoattribute\">System.Runtime.CompilerServices.TypeForwardedTo<\/a>. In addition, using <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.runtime.compilerservices.typeforwardedfromattribute\">System.Runtime.CompilerServices.TypeForwardedFrom<\/a> attribute, one can set up a reverse link.<\/p>\n<p>&#8220;Type Forwarding To&#8221; links invoke a special action\u00c2\u00a0with the metasystem. During type resolution, the metasystem will try to find all the types that are used by a given type. A type definition could exist in another assembly, and so it is marked as to where the system can find it. Normally, if you use a type, it just puts in a reference directly to the assembly that contains the typedef. For &#8220;Type Forward To&#8221;, one can create an indirect typedef in the assembly.<\/p>\n<p>A nice description is at Redgate Hub, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/blogs\/anatomy-of-a-net-assembly-type-forwards\/\">here<\/a> (and a description of PE files <a href=\"https:\/\/www.red-gate.com\/simple-talk\/blogs\/anatomy-of-a-net-assembly-pe-headers\/\">here<\/a>). But, here is a simple example of how it may be used.<\/p>\n<p><em>Application assembly code<\/em><\/p>\n\n<p><em>Interface assembly code<\/em><\/p>\n\n<p><em>Implementation assembly code<\/em><\/p>\n\n<p>The best way to view what it does is to use DotPeek and look at all three assemblies produced. There, you can see in the interface assembly an ExportedType entry for Foobar.<\/p>\n<h3><em>So, what the hell is Net Standard?<\/em><\/h3>\n<p>Net Standard is in fact really just a standardized assembly, netstandard.dll, built for various frameworks. Because the version information for netstandard.dll is the same, e.g., 2.0.0.0. Each netstandard.dll (e.g., in\u00c2\u00a0C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App\\2.1.7\\netstandard.dll for Net Core 2.1, or\u00c2\u00a0C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\netstandard.dll for Net Framework 4.7.1) is an interface assembly, which contains links for all the types common across all platforms.<\/p>\n<p>So, for the type <em>Action<\/em>, the <em>ExportedType<\/em> entry is:<\/p>\n<ul>\n<li>netstandard.dll in Net Framework 4.7.1\n<ul>\n<li>TypeDefId=0, Name=Action, Namespace=System, Implementation=mscorlib<\/li>\n<\/ul>\n<\/li>\n<li>netstandard.dll in Net Core 2.1.7\n<ul>\n<li>TypeDefId=0, Name=Action, Namespace=System, Implementation=System.Runtime.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>Out of sight, out of mind?<\/h3>\n<p>\u00c2\u00a0Given that, there are two things that can trip you up. First, it&#8217;s possible to use reflection to call methods of a type not available directly through the API. For example,\u00c2\u00a0BitConverter.SingleToInt32Bits(float) isn&#8217;t in Net Standard 2.0, but I can call the method in Net Core 2.0;\u00c2\u00a0System.PlatformID has an attribute for\u00c2\u00a0Serializable in Net Framework 4.6.1, but it&#8217;s missing in Net Core 2.0; the class System.Runtime.InteropServices.RuntimeEnvironment is marked abstract in Net Core 2.0, but it&#8217;s missing in Net Framework 4.6.1.<\/p>\n<p>So, while you may compile something to Net Standard, it doesn&#8217;t mean that you can&#8217;t access details in the implementation framework.<\/p>\n<h3>Conclusion<\/h3>\n<p>Net Standard is at best an\u00c2\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Operational_definition\">operationally defined<\/a>\u00c2\u00a0specification, but I would never call it a specification at all because it&#8217;s C# code, and because it does not describe the semantics of the type. I would instead define Net Standard as an interchangeable, common\u00c2\u00a0<strong><em>interface library<\/em><\/strong>\u00c2\u00a0to a particular <strong><em>implementation library<\/em><\/strong>\u00c2\u00a0for the framework (e.g., Net Framework, Net Core, Mono, etc.) via <em><strong>type forwarding<\/strong><\/em>.\u00c2\u00a0<\/p>\n<p>That&#8217;s what the hell it is.<\/p>\n<p>\u00c2\u00a0<\/p>\n<p>[Abr 96] Abrial J.-R, The B-Book: Assigning Programs to Meanings, Cambridge University Press, 1996.<\/p>\n<p>[Luc 87] Lucas, Peter. &#8220;VDM: Origins, hopes, and achievements.&#8221;\u00c2\u00a0<i>International Symposium of VDM Europe<\/i>. Springer, Berlin, Heidelberg, 1987.<\/p>\n<p>\u00c2\u00a0<\/p>\n<p>\u00c2\u00a0<\/p>","protected":false},"excerpt":{"rendered":"<p>No ifs, ands, or buts, news about Net Standard may be old and stale, but one thing still applies: As Steve Martin would say, &#8220;What the hell is that?&#8221;\u00c2\u00a0Believe it or not, I started writing this entry years ago, a few months after Net Standard first came out. I had hoped that if I just &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/165.227.223.229\/index.php\/2019\/02\/01\/what-the-hell-is-that\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;What the hell is that?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/1578"}],"collection":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/comments?post=1578"}],"version-history":[{"count":0,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/1578\/revisions"}],"wp:attachment":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/media?parent=1578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/categories?post=1578"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/tags?post=1578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}