We get an assembly from a third party, e.g. Example.dll
. Recently the supplier introduced some breaking changes, but didn't change the name of the assembly. Can I rename the new assembly to e.g. Example2.dll
and load it dynamically using reflection? It is not strongly named.

- 35,875
- 47
- 158
- 240
-
1http://stackoverflow.com/questions/5640746/loading-renamed-c-sharp-assembly-throws-filenotfoundexception – Habib May 13 '14 at 16:42
-
2Of course you can rename it, since it's not strongly named, but that won't change the namespaces used, and it will conflict with the other assembly if they are loaded in the same AppDomain. – Nathan A May 13 '14 at 16:43
-
@NathanA I believe if he would use reflection he could actually still resolve these types correctly from the `Assembly` object. The code for this becomes very unwieldy very quickly, though – drew_w May 13 '14 at 16:46
-
@Habib, I guess this is what I am experiencing. Any idea how to get around it without recompiling the dll? – Grzenio May 13 '14 at 16:47
-
@Grzenio, I don't think that can be done without recompiling the dll. – Habib May 13 '14 at 16:49
-
@drew_w From my understanding, as soon as the assembly is referenced **in any way**, the assembly will be loaded into the current AppDomain. Once that happens, there is no way to differentiate between type names if they have the same name and same namespace. It's not allowed at compile time, and it wouldn't work at runtime either. – Nathan A May 13 '14 at 16:50
-
What is the ultimate goal? Why do you need both versions? – Sriram Sakthivel May 13 '14 at 16:51
3 Answers
Yes, you can rename it, since it's not strongly named, However, that won't change the namespaces the assembly uses, and it will conflict with the other assembly if they are loaded in the same AppDomain.
The only solution I can think of, assuming you want to use both versions at the same time, is to load it in a separate AppDomain and use proxies to make the calls. I'm not going to go into detail on how to do that though, as it can get very complex. My advise, push back to the vendor to get the breaking code corrected, or to give you a renamed assembly.
Here's a reference to get you started: http://msdn.microsoft.com/en-us/library/yk22e11a(v=vs.110).aspx

- 11,059
- 4
- 47
- 63
Yes you can load any assembly by name by calling Assembly.LoadFile
or Assembly.LoadFrom
or similar methods. The recommended method is LoadFrom
as it loads the assembly into the default load context and also loads the dependent assemblies. But remember that the actual AssemblyName does not change by file rename. There are tools out there that would change the assembly name for you as well though.

- 2,276
- 17
- 22
-
1
-
I'd start out with ILMerge. That program merges .net assemblies. I'm pretty sure it should be able to rename it. I'll do some research... – Farhad Alizadeh Noori May 13 '14 at 16:52
-
Am pretty sure IL merge is not going to help. You may find `Ildasm` and `Ilasm` helpful. You can rename the assembly and build new one, but do check if you're violating any license. – Sriram Sakthivel May 13 '14 at 16:53
-
@SriramSakthivel: Actually I think it should work fine. Select the original dll as the input assembly and specify the new name as the ouput assembly. It only has one file to merge so maybe it would just change the name. – Farhad Alizadeh Noori May 13 '14 at 16:57
-
What benefit you get in merging? You'll get a type name clash isn't it? – Sriram Sakthivel May 13 '14 at 16:59
-
No I'm guessing ILMerge would simply change the assemblyname when given only one input assembly. It wouldn't try to merge the assembly with itself so no clash. – Farhad Alizadeh Noori May 13 '14 at 17:23
I did exactly this with the AutoMapper dll. I needed both versions in my solution but just included the non-standard version in my application project (in a dll folder), and added this section to runtime part of the config file.
<dependentAssembly>
<assemblyIdentity name="AutoMapper" publicKeyToken="be96cd2c38ef1005" culture="neutral" />
<codeBase version="6.1.1.0" href="dlls\AutoMapper.dll" />
</dependentAssembly>
I just need to remember to manually update the dll folder if the 3rd party dll changes (we get it from Nuget).

- 4,342
- 6
- 50
- 87