Howto load assemby at runtime before AssemblyResolve event?

前端 未结 2 1145
日久生厌
日久生厌 2020-12-01 15:06

Actually i tried to implement some kind of \'statically linked\' assemblies, within my solution. So i tried the following:

  • Adding a reference to my assembly wi
相关标签:
2条回答
  • 2020-12-01 15:26

    Just in case you didn't know, there is a tool called ILMerge from MS Research that merges assemblies into one file.

    Also you can create Multi-file assemblies using the Assembly Linker tool.

    Plus to answer you original question, the problem I think is that the runtime does not know that the assembly you loaded manually is the one it should be looking for. So in the assembly resolve event instead of loading the assembly again, just pass back the reference to the assembly that you've manually loaded.

    0 讨论(0)
  • 2020-12-01 15:42

    The CLR Binder doesn't know that LoadMyAssemblies() does the same thing as the AssemblyResolve event, and that they are both trying to look for the same assembly and load it.

    AssemblyResolve event always gets fired at the point at which the Binder decides that it has searched all possible locations (that are searchable wrt that application) and could not find a match.

    This begs the original question, which is, why would you want to statically link your managed assemblies? Read this thread for a lot of discussion on this Static linking advantages

    I'll go ahead and answer the part on how to avoid hitting the AssemblyResolve event 1) Put the assembly into the GAC. As far as the Binder is concerned, the GAC always wins. 2) Place your assembly in the probing path and make sure that the Binder picks it up (look for the article 'How the runtime locates assemblies' on MSDN for more on this).

    0 讨论(0)
提交回复
热议问题