添加引用对编译代码有什么影响?
当我添加对 .dll 文件的引用时,项目的编译输出会发生什么变化?
(想象一下我已经添加了引用并重建。)
When I add a reference to a .dll file, what changes in the compiled output of a project?
(Just imagine I have added a reference and rebuild.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它只是一个引用(并且假设该dll是一个程序集)-则什么都没有;编译器会默默地删除未使用的引用,并且在您的场景中,您没有添加任何使用程序集的代码(即一些使用新 dll 中的类型的代码)。注意,我在这里稍微假设一下,新 dll 中(在已使用的命名空间中)没有扩展方法,可以为现有扩展方法的使用提供更好的匹配。
如果您已将引用标记为 Copy Local = True,那么在输出目录中您将获得额外的 dll(但在内部您的程序集将不会正式引用它 - 如果您的代码仍然会删除该引用)不接触组件)。
If it is just a reference (and assuming the dll is an assembly) - none whatsoever; unused references are silently dropped by the compiler and in your scenario you didn't add any code that uses the assembly (i.e. some code that uses types from the new dll). Note I'm making the slight assumption here that there were no extension methods in the new dll (in namespaces that were already in use) that provided better matches for existing extension method uses.
If you have marked the reference to Copy Local = True, then in your output directory you'll get the extra dll (but internally your assembly will not formally reference it - that reference is still dropped if your code doesn't touch the assembly).
清单将记录对
.dll
文件的引用 - 如果不使用它,编译器将在编译后的清单中删除该引用。所以,在这种情况下,没有影响。如果您在此库中定义了任何扩展方法,为您现有的(未修改的)代码提供更好的匹配,则这构成了对该库的使用,并且将使用扩展方法。
如果这不是 .NET 程序集,而是 com/com+ dll,也会生成一个包装器。
已编译程序集的 MSIL 部分不应发生任何其他变化。
The manifest will record the reference to the
.dll
file - if it is not used, the compiler will drop the reference in the compiled manifest. So, in this case, no impact.If you have defined any extension methods in this library that provide better matches for your existing (unmodified) code, this constitutes a use of this library and the extension methods will be used.
If this is not a .NET assembly, but a com/com+ dll, a wrapper will be generated as well.
Nothing else should change in regards to the MSIL portion of the compiled assembly.