Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the C# compiler decide to emit retargetable assembly references?

Retargetable assembly references have been introduced for the .NET Compact Framework and are now used to support Portable Class Libraries.

Basically, the compiler emits the following MSIL:

.assembly extern retargetable mscorlib
{
    .publickeytoken = (7C EC 85 D7 BE A7 79 8E )                         
    .ver 2:0:5:0
}

How does the C# compiler understand it has to emit a retargetable reference, and how to force the C# compiler to emit such reference even outside of a portable class library?

like image 546
Gael Fraiteur Avatar asked Jul 10 '12 07:07

Gael Fraiteur


People also ask

How does the C language work?

The C programming language works by writing your code into an executable file. The C compiler will take that executable and convert it in its entirety into machine code that will then be executed by your computer at runtime.

What does -> mean in C?

An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union. The arrow operator is formed by using a minus sign, followed by the greater than symbol as shown below. Syntax: (pointer_name)->(variable_name)


2 Answers

For the assembly itself, it's an assembly flag, ie [assembly: AssemblyFlags(AssemblyNameFlags.Retargetable)].

Make note that this flag is meaningless outside of platform assemblies - custom assemblies cannot be retargetable.

For references, it's copied as part of the name from the assembly being referenced.

like image 140
David Kean Avatar answered Oct 24 '22 03:10

David Kean


Not sure if this will help, but the following file was auto-generated and included in the build.

using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(
   ".NETPortable,Version=v4.0,Profile=Profile4", 
   FrameworkDisplayName = ".NET Portable Subset")]

This might hint to the compiler to do some magic.

Edit:

I think above makes a library portable. From the command line I can see /nostdlib+ is used, and a portable mscorlib.dll is referenced (which I assume has the same attribute as mentioned above).

"...\Program Files\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile4\mscorlib.dll"

like image 34
leppie Avatar answered Oct 24 '22 03:10

leppie