How to fix "Failed to resolve assembly"

3,313 views
Skip to first unread message

Yang Zhang

unread,
Jun 6, 2016, 1:37:26 PM6/6/16
to mono-cecil
Hi,

I am working on edit existing string/icon resource in assembly. I use mono.cecil to parse, edit the resource and save. Almost all the localized resource files work fine, but only the English original resource file does not work. The English project contains core logic code and references other dlls (OtherDLL.dll), this may cause exceptions when using mono.cecil, the test code and exception is below.

AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(resourceFileName);
// Some code
assemblyDefinition.Write(newFileName); // This will cause exception: "Failed to resolve assembly: 'OtherDLL, Version=10.1.1.1, Culture=neutral, PublicKeyToken=null'

The English dll is TestDll.dll under deployment directory, and other localized resource dlls are TestDll.resources.dll under localized directory, like /de/TestDll.resources.dll and /zh-CN/TestDll.resources.dll. (other localized resource files could be well parse and save, but English resource file failed.)

So how could I fix the issues? If it is the limitation or bug, could we use another walk around method to achieve the goal?

Thanks in advance,
Yang

Jb Evain

unread,
Jun 6, 2016, 1:52:35 PM6/6/16
to mono-...@googlegroups.com
The exception tells you that in order to write your assembly, Cecil needs to read OtherDLL.

This happens when there's a metadata element that requires to read the definition of a type in another assembly.

The solution is to pass an assembly resolver when reading the assembly that knows how to resolve OtherDLL.

It could look like this:

var resolver = new DefaultAssemblyResolver();
resolver.AddSearchDirectory("C:\path\to\directory\which\contains\other\");

var assembly = AssemblyDefinitiob.ReadAssembly(fileName, new ReaderParameters { AssemblyResolver = resolver });

Jb

--
--
--
mono-cecil
---
You received this message because you are subscribed to the Google Groups "mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mono-cecil+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yang Zhang

unread,
Jun 8, 2016, 1:27:35 AM6/8/16
to mono-cecil
Thanks. It is really a perfect solution for me, and now it could fix my issue.

Regards,
Yang
Reply all
Reply to author
Forward
0 new messages