Linking a precompiled MSIL file into an assembly?

Is there any way to precompile an MSIL file and then have the .net linker link that into a .net assembly at compile time?

So, for example. I have a project. I compile it, decompile it to MSIL, and tweak the msil.

Now then, I'd like to compile and link that tweaked msil file into another project.

Yes, I +COULD+ recompile it as a seperate assembly and just reference it, but in this particular case, i can't do that. The contents of the MSIL needs to be actually in the target assembly.

Any ideas if this is even possible? So far, I've had no luck finding anything.

-- EDIT-- One suggestion is ILMerge, which might work. I'll grabbing the latest copy now and will see how that fairs. The only issue I have with that is that in the past, I've found debugging to be difficult after the merge (or at the very least, more difficult than if I didn't use ILmerge). I was hoping to possibly tweak the proj file to include the compiled MSIL directly as part of the build process, so everything would continue to be easily debugged in the IDE.

-- EDIT -- Well, doesn't look like ILMerge will work for this scenario First, I got this message: An exception occurred during merging: (TaskId:32) ILMerge.Merge: The assembly 'Blah' is not marked as containing only managed code. (TaskId:32) (Consider using the /zeroPeKind option -- but read the documentation first!) (TaskId:32) at ILMerging.ILMerge.Merge() (TaskId:32) at ILMerging.ILMerge.Main(String[] args) (TaskId:32)

Ok. So, what the heck, I add the /zeropekind and try again.

No error!

But, in this particular case, the DLL being merged in has several functions tweaked in the MSIL to be exported as CDECL functions. In the merged dll, they are no longer exported.

Rats.

Back to square one.


You can use ILMerge to combine multiple assemblies into one, so you could tweak your msil, make it a seperate assembly, then ILMerge it into your first one.

Another alternative would be to load your tweaked MSIL as an embedded resource in your first assembly (which would mean your tweaked msil assembly would have to be created before the one that references it) then at runtime you can read the byte[] of the embedded resource into an Assembly, loaded into the AppDomain.

A third, more drastic approach would be to decompile both assemblies, and combine the output msil files into 1, and rebuild it into an assembly. You would just have to watch out for naming conflicts between the two sets of msil code, but since you can dump the msil to a flat text file, it shouldn't be that difficult to write some utility program to recombine them... although, thats basically exactly what ILMerge is, except it starts with 2 assemblies, instead of msil code in a text file...


Like previously mentioned, use ILMerge to merge the assemblies.

Also, you can use the DLLExport utility to expose functions marked as CDECL.

See these links:
http://www.darinhiggins.com/self-registering-com-net-assemblies/
http://www.vbfengshui.com/exposing-c-style-entry-points-in-a-net-assembly-revisited/

链接地址: http://www.djcxy.com/p/36066.html

上一篇: 什么使Visual Studio调试器停止评估ToString重写?

下一篇: 将预编译的MSIL文件链接到程序集中?