问题
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?
回答1:
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.
回答2:
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"
回答3:
I've noticed by experimenting that the C# compiler would make an reference compiler as retargetable if the referenced assembly is marked as retargetable (a modifier on the .assembly
section in MSIL). I did not find how the compiler decides to make the assembly retargetable, yet.
来源:https://stackoverflow.com/questions/11408792/how-does-the-c-sharp-compiler-decide-to-emit-retargetable-assembly-references