C#编译/构建增量过程吗? [英] Is C# compile/build an incremental process?

查看:65
本文介绍了C#编译/构建增量过程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的解决方案包含许多C#项目。它们之间有复杂的依赖关系,例如。项目A / B / C,A依赖B,B依赖C。如果我更改项目C中的一个文件,然后重新生成解决方案,则项目A,B,C将一起重新构建。

Our solution contains lots of C# projects. There are complicated dependency relationship between them, eg. project A/B/C, A dependents on B, B dependents on C. If I change one file in project C, then rebuild the solution, project A,B,C will be rebuild together.

在C ++中,内部版本包含两个过程,即编译和链接。如果我在项目C中更改一个文件,那么我将构建解决方案,A和B中的相关文件将被编译(其他文件将不被编译,它们的.obj将在链接过程中重复使用),然后执行链接。

In C++, build contains two process, compile and link. If I change one file in project C, then I build the solution, the relevant file in A and B will be compiled(other's files won't be compiled, their .obj will be reused in link process), then do link.

在Java中,将仅重新编译项目C中更改的文件,将其他文件保留下来,然后打包到.jar中。它会重用以前的工作输出(未更改文件的.class)。

In java, just the changed file in project C will be recompiled, others file will be kept then package to .jar. It reuse previous work output(not changed file's .class).

总而言之,C#不会重复使用任何以前的工作输出。它不像Java的.class和C ++的.obj一样具有任何中间文件。因此,在这一点上,我觉得C#不会进行增量构建过程。一些小的更改将导致构建过程变大。我不明白为什么C#不使用以前的工作输出来加快构建过程。

In a word, C# doesn't reuse any previous work output. It doesn't have any middle file just like Java's .class and C++'s .obj. So in this point, I feel C# doesn't do incremental build process. Some little change will cause a big build process. I don't understand why C# doesn't use previous work output to accelerate the build process.

我不确定我对C#编译/构建过程的理解是否正确。您能帮忙解释一下吗?非常感谢。

I am not sure whether my understanding of C# compile/build process is right. Could you please help to explain more? Thanks a lot.

推荐答案

C#编译器会进行增量编译,我不确定您在哪里有一个想法,那就不是。也许由于解决方案的复杂性,您无法正确理解依赖关系,实际上您认为不需要重新编译的项目也是必需的。

The C# compiler does incremental compilations, I'm not sure where you got the idea that it doesn't. Maybe, due to the complexity of your solution, you are not understanding the dependencies correctly and projects that you assumed would not need to be recompiled are in fact necessary.

最好检查编译器行为的方法是创建一个简单的虚拟解决方案并对其进行处理:

The best way to check the compiler's behavior is to create a simple dummy solution and play around with it:

设置:


  1. 创建一个空的Visual Studio C#解决方案。

  2. 添加任意两个项目, A B

  3. 使项目 B 在项目中成为引用 A

  4. 中实现类 FooInB B 并在 A BarInA 中的另一个类中使用它。

  1. Create an empty Visual Studio C# solution.
  2. Add any two projects, A and B.
  3. Make project B a reference in project A.
  4. Implement a class FooInB in B and use it in another class in A BarInA.

现在让我们开始以下设置:

Now lets play around a bit with this setup:


  1. 编译解决方案。您将看到两个项目都已编译。

  2. 再次编译解决方案。您将看到没有一个项目可以编译,都是最新的。

  3. BarInA 中更改实现,然后再次编译。您
    会看到个项目在编译, A 。由于没有更改,因此不需要
    再次编译 B

  4. 更改<$ c $中的实现c> FooInB 并最后一次编译。您将看到两个项目都已编译。此行为是正确的, A 取决于 B ,因此 B 必然需要再次重新编译 A ,以确保它指向最新版本的 B 。在理论上, C#编译器可以检测 B 中的更改是否对 A ,因此可能再次优化构建 A ,这将是一个噩梦,每个项目都可能引用不同且过时的程序集版本。

  1. Compile the solution. You will see that both projects compile.
  2. Compile the solution again. You will see that none of the projects compile, both are up to date.
  3. Change the implementation in BarInA and compile again. You will see that only one project compiles, A. There is no need to compile B again as there are no changes.
  4. Change the implementation in FooInB and compile one last time. You will see that both projects compile. This behaviour is correct, A depends on B so any change in B will necessarily need that A recompile again to make sure it is pointing to the latest version of B. In a theoretical world where the C# compiler could detect if the changes in B have no consequences in A and could therefore "optimize" away building A again, would be a nightmare scenario where each project could be referencing different and outdated assembly versions.

我要指出的是,AFAIK,C#编译器仅在 project 级别执行增量编译。我不知道任何给定程序集中的类级别的任何增量编译优化。精通编译器内部工作的人也许可以澄清这种行为。

That said, I'd like to point out that, AFAIK, the C# compiler will only perform incremental compilations at project level. I am not aware of any incremental compilation optimizations at class level inside any given assembly. Someone with much more insight in the inner workings of the compiler might be able to clarify this behavior.

这篇关于C#编译/构建增量过程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆