简体   繁体   English

编译.NET程序集时使用netmodules的用例?

[英]Use cases for using netmodules when compiling .NET assemblies?

I'm interested in use cases for netmodules in .NET. 我对.NET中netmodules的用例很感兴趣。 In particular, I've been looking into better ways to break up solutions in .NET but not have quite as many assemblies to deploy. 特别是,我一直在寻找更好的方法来破解.NET中的解决方案,但没有足够多的程序集来部署。 Netmodules are a very interesting idea, but they appear to break debugging and are not natively supported in Visual Studio (though they are for MSBuild) . Netmodules是一个非常有趣的想法,但它们似乎打破了调试,并且在Visual Studio中本身不受支持(尽管它们用于MSBuild) I'd prefer to rely on something native to .NET, so ILMerge, while interesting isn't what I really want. 我宁愿依赖原生的.NET,所以ILMerge,虽然有趣并不是我真正想要的。

For some of my own projects, I'm also beginning to use FAKE , which allows for some interesting build steps, such as separating out test files . 对于我自己的一些项目,我也开始使用FAKE ,它允许一些有趣的构建步骤,例如分离出测试文件 In other words, writing custom compile steps is not a problem. 换句话说,编写自定义编译步骤不是问题。

A netmodule on its own is practically worthless: Their types and code cannot be loaded and executed by the runtime environment. netmodule本身几乎一文不值:它们的类型和代码无法由运行时环境加载和执行。 You can only load types and execute code from assemblies , so it's quite likely that you will eventually combine several netmodules into a (multi-file) assembly. 您只能从程序集中加载类型和执行代码,因此很可能最终将多个netmodule组合到一个(多文件)程序集中。 So let's take a look at the benefits of these. 那么让我们来看看这些的好处。

Jeffrey Richter mentions three uses for multi-file assemblies in his book CLR via C# (p. 44), two of which result from the use of netmodules: Jeffrey Richter在他的书籍CLR中通过C# (第44页)提到了多文件程序集的三种用法,其中两种是使用netmodules:

  1. "You can partition your types among separate files, allowing for files to be incrementally downloaded […]. Partitioning the types into separate files also allows for partial or piecemeal packaging and deployment for applications you purchase and install." “您可以在不同的文件中对类型进行分区,允许逐步下载文件[...]。将类型分区为单独的文件还允许对您购买和安装的应用程序进行部分或零碎的打包和部署。”

    At least Microsoft's implementation of the CLI (.NET) seems to load multi-file assembly incrementally, and not in its entirety right from the start. 至少微软的CLI(.NET)实现似乎是逐步加载多文件组件,而不是从一开始就完全加载。 A module from an assembly only appears to get loaded from disk (or from the network?) when a type inside it is actually needed. 当组件中的模型实际需要时,组件中的模块似乎只能从磁盘(或从网络?)加载。

  2. "You can create assemblies consisting of types implemented in different programming languages. […]" “你可以创建由不同编程语言实现的类型组成的程序集。[...]”

    I'm not certain that this actually adds much value in real-world scenarios, (a) because Visual Studio doesn't support project references to netmodules, and (b) because you can get the same benefit with assemblies. 我不确定这实际上在实际场景中增加了很多价值,(a)因为Visual Studio不支持对netmodules的项目引用,(b)因为你可以获得与程序集相同的好处。

    There's one notable difference between a multi-assembly and multi-file assembly approach: One assembly cannot by default access another assembly's types that have internal / Friend (ie assembly) visibility. 多组件和多文件组装方法之间存在一个显着差异:一个组件默认情况下不能访问具有internal / Friend (即组件)可见性的另一个组件类型。 If you were compiling to modules instead and then link them into a single multi-file assembly, a module compiled from C# could access internal types of a module compiled with VB.NET (and vice versa). 如果您正在编译模块而不是将它们链接到单个多文件程序集中,则从C#编译的模块可以访问使用VB.NET编译的模块的internal类型(反之亦然)。

    You'll find a brief demonstration of this below. 您将在下面找到一个简短的演示。


    CsharpClass.cs: CsharpClass.cs:

     internal class CsharpClass { } 

    VbClass.vb: VbClass.vb:

     Friend Class VbClass : End Class 

    Program.cs: Program.cs中:

     public static class Program { public static void Main() { var vbClass = new VbClass(); var csharpClass = new CsharpClass(); } } 

    Build script for netmodules: 构建netmodules的脚本:

     csc.exe /t:module /out:CsharpClass.netmodule CsharpClass.cs vbc.exe /t:module /out:VbClass.netmodule VbClass.vb csc.exe /t:exe /out:Program.exe /addmodule:CsharpClass.netmodule /addmodule:VbClass.netmodule Program.cs 

    This build will work and execute without any errors. 此构建将无任何错误地工作和执行。

    Note that there is nothing magical about the .netmodule file extension; 请注意, .netmodule文件扩展名没有什么神奇之处; this is only a convention, but the output file is a regular .NET DLL. 这只是一个约定,但输出文件是一个常规的.NET DLL。

    Build script for assemblies: 构建程序集的脚本:

     csc.exe /t:library /out:CsharpClass.dll CsharpClass.cs vbc.exe /t:library /out:VbClass.dll VbClass.vb csc.exe /t:exe /out:Program.exe /r:CsharpClass.dll /r:VbClass.dll Program.cs 

    This build will fail because: 此构建将失败,因为:

     Program.cs(5,27): error CS0122: 'VbClass' is inaccessible due to its protection level Program.cs(5,23): error CS0143: The type 'VbClass' has no constructors defined Program.cs(6,31): error CS0122: 'CsharpClass' is inaccessible due to its protection level Program.cs(6,27): error CS0143: The type 'CsharpClass' has no constructors defined 

Can you use Jeffrey Richter's assembly embedding technique ? 你能用杰弗里里希特的装配嵌入技术吗? I haven't tried it myself, but it looks promising. 我自己没试过,但看起来很有希望。

The lack of responses makes me think that the other options noted in the comments and answers are generally considered better approaches than netmodules. 缺乏回应使我认为评论和答案中提到的其他选项通常被认为是比netmodules更好的方法。 By that, I assume that means no one actually uses or knows of any good uses for netmodules. 通过这种方式,我认为这意味着没有人真正使用或知道网络模块的任何良好用途。

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

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