简体   繁体   English

C#编译器如何决定发出可重定向的程序集引用?

[英]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. .NET Compact Framework引入了可重定向的程序集引用,现在用于支持可移植类库。

Basically, the compiler emits the following MSIL: 基本上,编译器会发出以下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? C#编译器如何理解它必须发出一个可重定向的引用,以及如何强制C#编译器甚至在可移植类库之外发出这样的引用?

For the assembly itself, it's an assembly flag, ie [assembly: AssemblyFlags(AssemblyNameFlags.Retargetable)]. 对于程序集本身,它是一个程序集标志,即[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. 对于引用,它被复制为被引用的程序集中名称的一部分。

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). 从命令行我可以看到/nostdlib+被使用,并且引用了一个可移植的mscorlib.dll (我假设它具有与上面提到的相同的属性)。

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

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). 通过实验我已经注意到,如果引用的程序集被标记为retargetable(MSIL中.assembly部分的修饰符),C#编译器会将引用编译器设置为retargetable。 I did not find how the compiler decides to make the assembly retargetable, yet. 我没有找到编译器如何决定使程序集重定向。

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

相关问题 Mono MSC C# 编译器如何添加程序集引用 - Mono MSC C# compiler how to add assembly references 使用运算符重载时,C#编译器如何确定哪种实现适用于混合类型操作数? - With operator overloading, how does the C# compiler decide which implementation applies with mixed-type operands? 为什么C#编译器在IL中发出额外的OpCode? - Why does C# compiler emit additional OpCodes in IL? C#编译器在编译期间是否解析引用的引用? - Does c# compiler resolve reference of references during compilation? c#编译器会将其转换为c还是intel汇编代码? - Does c# compiler turns it into c or intel assembly code? C#/。NET程序集引用如何断开连接? - How do C#/.NET assembly references get disconnected? MSBuild如何决定是否需要重建C#库? - How does MSBuild decide whether it needs to rebuild a C# library or not? 当使用具有new()约束的泛型类型调用new时,为什么c#编译器会发出Activator.CreateInstance? - Why does the c# compiler emit Activator.CreateInstance when calling new in with a generic type with a new() constraint? 如何获取C#.NET编译器程序集GUID - How to get C# .NET Compiler Assembly GUID 在C#中,如何使用Emit调用静态方法 - In C# how does one call a static method using emit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM