简体   繁体   English

下面的“重新编译程序集”是什么意思?| C#

[英]What does it mean by "recompile assemblies" in the following?| C#

I faced the follwing section when reading a book about C#:我在读一本关于 C# 的书时遇到了以下部分:

Constants should be avoided for two important reasons: the value must be known at compile time, and it must be expressible as a literal string, Boolean, or number value.应避免使用常量有两个重要原因:值必须在编译时已知,并且必须可以表示为文字字符串 Boolean 或数字值。 Every reference to the const field is replaced with the literal value at compile time, which will, therefore, not be reflected if the value changes in a future version and you do not recompile any assemblies that reference it to get the new value.对 const 字段的每个引用在编译时都被替换为文字值,因此,如果该值在未来版本中发生更改并且您不重新编译任何引用它的程序集以获取新值,则不会反映出来。

I have two questions now.我现在有两个问题。

1- What does it mean by the highlited part "future version" above? 1- 上面突出显示的部分“未来版本”是什么意思?

2- What does it mean when it says: you do not recompile any assemblies... ? 2- 当它说:你不重新编译任何程序集时是什么意思...... I didn't get it when I saw this part, can anyone explain that how an assembly get recompiled?当我看到这部分时我没明白,谁能解释一下程序集是如何重新编译的?

Let's say you maintain a class library named ProjectA .假设您维护一个名为ProjectA的 class 库。 You include a constant named SomeUtil.AConstantValue and set it to an int value of 1 .您包括一个名为SomeUtil.AConstantValue的常量并将其设置为1int值。 You release this as AssemblyVersion 1.0.0.0 and FileVersion 1.0.0.0.您将其发布为 AssemblyVersion 1.0.0.0 和 FileVersion 1.0.0.0。

ProjectA 1.0.0.0

Now let's say someone consumes your library project in ProjectB and uses the value of SomeUtil.AConstantValue .现在假设有人在ProjectB中使用您的库项目并使用SomeUtil.AConstantValue的值。 They compile their assembly, which copies the value 1 into the MSIL of the assembly.他们编译他们的程序集,将值 1复制到程序集的 MSIL 中。 But that is fine at this point, because it is the same as ProjectA .但此时这很好,因为它与ProjectA相同。

ProjectB 1.0.0.0
   |
    ---- ProjectA 1.0.0.0

Next, you discover that the value 1 is not a high enough value for ProjectA 's SomeUtil.AConstantValue to work correctly.接下来,您发现值 1 不足以让ProjectASomeUtil.AConstantValue正常工作。 So, you change the value to a 2 and release the patch with AssemblyVersion 1.0.0.0 and and FileVersion 1.1.0.0.因此,您将值更改为 2 并使用 AssemblyVersion 1.0.0.0 和 FileVersion 1.1.0.0 发布补丁。

ProjectA 1.1.0.0

The author of ProjectB sees you have released a new version of ProjectA and decides to xCopy (fancy speak for copy and paste) the DLL to upgrade it. ProjectB的作者看到您发布了新版本的ProjectA并决定 xCopy(花哨的说法是复制和粘贴)DLL 对其进行升级。 Since it has an AssemblyVersion of 1.0.0.0, it is binary compatible so there is no need to recompile ProjectB .由于它的 AssemblyVersion 为 1.0.0.0,因此它是二进制兼容的,因此无需重新编译ProjectB However, since the value of 1 was copied into ProjectB when it was compiled, it doesn't see the new constant value.但是,由于 1 的值在编译时被复制到ProjectB中,因此它看不到新的常量值。

ProjectB 1.0.0.0 (note the version here didn't change because it wasn't recompiled)
   |
    ---- ProjectA 1.1.0.0

If you imagine the same scenario, but use a static field instead of a constant, ProjectB will immediately start using the new value 2 for SomeUtil.AConstantValue .如果您想象相同的场景,但使用 static 字段而不是常量, ProjectB将立即开始使用SomeUtil.AConstantValue的新值 2。 This is a better situation, because there won't be a difference in behavior if ProjectB is recompiled vs the new DLL simply copied along with ProjectB .这是一个更好的情况,因为如果重新编译ProjectB与简单地与ProjectB一起复制的新 DLL ,行为不会有差异。

NOTE: Generally speaking, this scenario doens't play out very often if you use NuGet because most people will recompile the application when they get the new version.注意:一般来说,如果您使用 NuGet,这种情况不会经常发生,因为大多数人在获得新版本时会重新编译应用程序。 The advice given is to cover cases where the DLL is unzipped from the NuGet package and copied to the bin/app folder manually or installed into the Global Assembly Cache.给出的建议是涵盖 DLL 从 NuGet package 中解压缩并手动复制到 bin/app 文件夹或安装到全局程序集缓存中的情况。 Any of these scenarios can be done without forcing a recompile of ProjectB .这些场景中的任何一个都可以在不强制重新编译ProjectB的情况下完成。

Answers答案

  1. What does it mean by the highlited part "future version" above?上面突出显示的部分“未来版本”是什么意思?

    Using the above scenario, the "future version" referred to would be FileVersion 1.1.0.0.使用上述场景,所指的“未来版本”将是 FileVersion 1.1.0.0。 It is considered a "future version" here, because ProjectB was compiled against FileVersion 1.0.0.0 (in the past).它在这里被认为是“未来版本”,因为ProjectB是针对 FileVersion 1.0.0.0(过去)编译的。

  2. What does it mean when it says: you do not recompile any assemblies...?当它说:你不重新编译任何程序集......时,它是什么意思?

    In the above scenario, the author of ProjectB decided to copy the ProjectA DLL you authored into their application directory.在上述场景中, ProjectB的作者决定将您编写的ProjectA DLL 复制到他们的应用程序目录中。 This overwrites FileVersion 1.0.0.0.这将覆盖 FileVersion 1.0.0.0。 Most of the code changes that were made to ProjectA will immediately start being used by replacing the DLL (assembly).ProjectA所做的大部分代码更改将通过替换 DLL(程序集)立即开始使用。 However, the constant value would not be utilized unless ProjectB is recompiled while referencing ProjectA .但是,除非在引用ProjectA时重新编译ProjectB ,否则不会使用常量值。

I didn't get it when I saw this part, can anyone explain that how an assembly get recompiled?当我看到这部分时我没明白,谁能解释一下程序集是如何重新编译的?

C# is one of many different compiled languages. C# 是许多不同的编译语言之一。 With a compiled language the source code that is written is compiled into a format that more closely represents the machine code that the computer uses.使用编译语言,将编写的源代码编译成一种更接近于代表计算机使用的机器代码的格式。 .NET compiles down to a format called intermediate language (IL or MSIL), which is what usually gets deployed to the target device. .NET 编译成一种称为中间语言(IL 或 MSIL)的格式,通常将其部署到目标设备。 In general, compiling makes the code run faster than if it is simply interpreted on demand.通常,编译使代码运行速度比简单地按需解释要快。

There are compilers built into Integrated Development Environments (IDEs) such as Visual Studio , Visual Studio Code , or JetBrains Ryder , as well as software development kits (SDKs) that can compile on the command line (for example, the .NET SDK which has a compile command do.net build ).集成开发环境 (IDE) 中内置了编译器,例如Visual StudioVisual Studio CodeJetBrains Ryder ,以及可以在命令行上编译的软件开发工具包 (SDK)(例如, .NET SDK ,它具有编译命令do.net build )。

References参考

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM