ilmerge使用_使用ILMerge和MSBuild在Visual Studio中的单个程序集中无缝混合语言

ilmerge使用

ilmerge使用

I really like the new LINQ to XML direct language support in VB9. However, while it's cool, I'm not ready (or willing) to dump C# and start using VB. But, if I could only use VB9 just for the XML stuff...

我真的很喜欢VB9中新的LINQ to XML直接语言支持。 但是,虽然很酷,但我还没有准备好(或不愿意)转储C#并开始使用VB。 但是,如果我只能将VB9用作XML的话……

Sure, I could create a VB assembly and a C# assembly and add them to a solution, but then I'd have two assemblies. I could add a PostBuildEvent batch file and call ILMerge myself (merging the two assemblies into one), but that just doesn't seem pure enough.

当然,我可以创建一个VB程序集和一个C#程序集并将它们添加到解决方案中,但是那时我将有两个程序集。 我可以添加一个PostBuildEvent批处理文件,然后自己调用ILMerge (将两个程序集合并为一个),但这似乎还不够纯。

solution

What I really want is to be able to mark an assembly as merge-able and have it automatically merged in just because it's referenced.

我真正想要的是能够将一个程序集标记为可合并,并使其仅由于被引用而自动合并。

Here's what I came up with. Thanks to Dan Moseley and Joshua Flanagan for their help. Thanks to Jomo Fisher for the Target File.

这是我想出的。 感谢Dan MoseleyJoshua Flanagan的帮助。 感谢Jomo Fisher提供的目标文件。

First, here's the sample project. There's a C# MergeConsole.exe that references a C# MainLibraryILMerge.dll that references a VB XmlStuffLibrary.

首先,这是示例项目。 有一个引用C#MainLibraryILMerge.dll的C#MergeConsole.exe,该C#MainLibraryILMerge.dll引用了一个VB XmlStuffLibrary。

If I compile it as it is, I get a folder structure that includes three resulting assemblies.

如果按原样进行编译,则会得到一个包含三个结果程序集的文件夹结构。

folder1

Now, here's were we start messing around. Remember, we don't want to have the extra VB-specific XmlStuffLibrary right? We just want to use the XML features.

现在,这是我们开始胡闹的地方。 记住,我们不想拥有额外的VB专用XmlStuffLibrary吗? 我们只想使用XML功能。

First, download and install ILMerge in the standard location.

首先,在标准位置下载并安装ILMerge

Now, go to C:\Program Files\MSBuild (or C:\Program Files (x86)\MSBuild on x64) and make a new text file called "Ilmerge.CSharp.targets". This file is the start of a hack we're going to borrow from Jomo Fisher.

现在,转到C:\ Program Files \ MSBuild(或x64上的C:\ Program Files(x86)\ MSBuild),然后创建一个名为“ Ilmerge.CSharp.targets”的新文本文件。 这个文件是我们要从乔莫·费舍尔借来的黑客的开始。

Note the red bits in the file below. We're creating an "AfterBuild" target...a Post Build Event in the MSBUILD world.

请注意以下文件中的红色位。 我们正在创建一个“ AfterBuild”目标……MSBUILD世界中的一个Post Build事件。

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 
    
  <Target Name="AfterBuild">
    <CreateItem Include="@(ReferencePath)" Condition="'%(CopyLocal)'=='true' and '%(ReferencePath.IlMerge)'=='true'">
      <Output TaskParameter="Include" ItemName="IlmergeAssemblies"/>
    </CreateItem>
    <Message Text="MERGING: @(IlmergeAssemblies->'%(Filename)')" Importance="High" />
    <Exec Command="&quot;$(ProgramFiles)\Microsoft\Ilmerge\Ilmerge.exe&quot; /out:@(MainAssembly) &quot;@(IntermediateAssembly)&quot; @(IlmergeAssemblies->'&quot;%(FullPath)&quot;', ' ')" />
  </Target>
  <Target Name="_CopyFilesMarkedCopyLocal"/>
</Project>

In this target, we're going to look for assemblies that are marked CopyLocal but also that have IlMerge equal to true. Then we'll call IlMerge passing in those referenced assemblies.

在此目标中,我们将查找标记为CopyLocal但IlMerge等于true的程序集。 然后,我们将调用IlMerge传入那些引用的程序集。

Is this some undocumented MSBUILD thing? No, you can put whatever you want in an MSBUILD file and refer to it later. Since CSPROJ files (Visual Studio Projects) are MSBUILD files, we can open it in Notepad.

这是一些未记录的MSBUILD东西吗? 不,您可以将所需的任何内容放入MSBUILD文件中,以后再引用它。 由于CSPROJ文件(Visual Studio项目)是MSBUILD文件,因此我们可以在记事本中打开它。

Open the CSPROJ for the C# project that references the VB one and make these changes: 

打开引用VB的C#项目的CSPROJ,并进行以下更改:

... 
<ItemGroup>
   <ProjectReference Include="..\XmlStuffLibrary\XmlStuffLibrary.vbproj">  
    <Project>{someguid}</Project>    
    <Name>XmlStuffLibrary</Name>
    <Private>True</Private>
    <IlMerge>True</IlMerge>   </ProjectReference>
</ItemGroup>  
<Import Project="$(MSBuildExtensionsPath)\Ilmerge.CSharp.targets" /> <!-- <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> --> ...

... <ItemGroup> <ProjectReference Include =“ .. \ XmlStuffLibrary \ XmlStuffLibrary.vbproj”> <Project> { someguid } </ Project> <Name> XmlStuffLibrary </ Name> <Private> True </ Private> <IlMerge>真</ IlMerge> </ ProjectReference> </ ItemGroup> <导入项目=“ $(MSBuildExtensionsPath)\ Ilmerge.CSharp.targets” /> <!- <导入项目=“ $(MSBuildToolsPath)\ Microsoft.CSharp.targets” />-> ...

At the bottom there, we're commenting out Microsoft.CSharp.targets (but notice that it's included back in at the top of the new  Ilmerge.CSharp.targets.

在底部,我们正在注释Microsoft.CSharp.targets(但请注意,它已包含新Ilmerge.CSharp.targets的顶部。

Then a totally made-up element <IlMerge> is added. That's the most important part. We're saying we want this specific Reference (or References) merged into the final assembly. This made-up element is referenced in the conditional "'%(ReferencePath.IlMerge)" above in the AfterBuild.

然后添加一个完全构成的元素<IlMerge>。 那是最重要的部分。 我们是说要将此特定参考(或多个参考)合并到最终程序集中。 在AfterBuild中的以上条件“ '%(ReferencePath.IlMerge) ”中引用了此组成元素。

The addition of this IlMerge element to Jomo's original hack gives me the flexibility to pick which references I want to merge in, and in my specific case, I'll use the VB9 new XML hotness inside my C# assemblies. Schwing.

将这个IlMerge元素添加到Jomo的原始代码后,使我可以灵活地选择要合并的引用,在我的特定情况下,我将C#程序集中使用VB9新的XML hotness。 施温。

Close and save. If you're running Visual Studio, switch back there and it'll prompt you to Reload your project file.Because we've messed it it, you'll get this warning dialog. Select "Load project normally" and click OK.

关闭并保存。 如果您运行的是Visual Studio,请切换回该位置,它会提示您重新加载项目文件。由于我们将其弄乱了,因此会出现此警告对话框。 选择“正常加载项目”,然后单击“确定”。

Security Warning for MainLibraryILMerge

At this point we can build either from the command-line using MSBUILD on the Solution (SLN) file, but more importantly we can (of course) build from inside Visual Studio.

至此,我们可以使用解决方案(SLN)文件上的MSBUILD从命令行进行构建,但更重要的是,我们可以(当然)可以从Visual Studio内部进行构建。

You can see the output in the Output Window in VS.NET.

您可以在VS.NET的“输出”窗口中看到输出。

MERGING: XmlStuffLibrary
"C:\Program Files (x86)\Microsoft\Ilmerge\Ilmerge.exe"
  /out:bin\Debug\MainLibrary.dll "obj\Debug\MainLibrary.dll"   
  "C:\dev\CSharpVBTogetherAtLast\MergeConsole\XmlStuffLibrary
  \bin\Debug\XmlStuffLibrary.dll"

合并:XmlStuffLibrary “ C:\ Program Files(x86)\ Microsoft \ Ilmerge \ Ilmerge.exe” /out:bin\Debug\MainLibrary.dll“ obj \ Debug \ MainLibrary.dll” “ C:\ dev \ CSharpVBTetherAtLast \ MergeConsole \ XmlStuffLibrary \ bin \ Debug \ XmlStuffLibrary.dll“

And the results in the bin folder:

结果在bin文件夹中:

folder2

Looks like the VB XmlStuffLibrary is gone! But where it is? Let's load up MainLibrary in Reflector:

看起来VB XmlStuffLibrary已经消失了! 但是它在哪里? 让我们在Reflector中加载MainLibrary:

reflector1

Looks like they are both in there. To refresh, if I want to merge in VB assemblies:

看起来他们俩都在那儿。 要刷新,如果我想在VB程序集中合并:

  • Change the referencing CSPROJ to import "IlMerge.CSharp.targets"

    更改引用CSPROJ以导入“ IlMerge.CSharp.targets”

  • Add <IlMerge>True</IlMerge> to the references you want merged in.

    将<IlMerge> True </ IlMerge>添加到要合并的引用中。
  • Save, Reload, Build

    保存,重新加载,构建

C# and VB, living together, mass hysteria!

C#和VB,在一起生活,歇斯底里!

翻译自: https://www.hanselman.com/blog/mixing-languages-in-a-single-assembly-in-visual-studio-seamlessly-with-ilmerge-and-msbuild

ilmerge使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值