Skip to content

Mono.Cecil's AssemblyResolver should search target assembly's directory#10

Merged
einaregilsson merged 1 commit into
kzu:masterfrom
bfriesen:assembly_resolver
Mar 4, 2016
Merged

Mono.Cecil's AssemblyResolver should search target assembly's directory#10
einaregilsson merged 1 commit into
kzu:masterfrom
bfriesen:assembly_resolver

Conversation

@bfriesen
Copy link
Copy Markdown
Contributor

@bfriesen bfriesen commented Mar 4, 2016

If an assembly that is to be modified by InjectModuleInitializer contains attributes with constructor parameters or properties of a type defined in a third-party library, Mono.Cecil will often fail to load that third-party assembly. This is the case because, by default, Mono.Cecil only searches for assembly files in "." and "bin", relative to Directory.GetCurrentDirectory() (see exhibit A and exhibit B). Unless the current directory is the same directory as the assembly to be modified (which is not the case in the InjectModuleInitializer nuget package), this causes a failure with a "Failed to resolve Assembly" message.

For example, if the to-be-modified assembly has a dependency on Newtonsoft.Json, and it has classes defined like the following, InjectModuleInitializer will fail:

// MemberSerialization.OptIn is defined in Newtonsoft.Json.dll and can't be located by
// Mono.Cecil. It isn't the JsonObject attribute, but rather its constructor parameter that
// is the problem. InjectModuleInitializer will fail if a class like this is defined.
[JsonObject(MemberSerialization.OptIn)]
public class Foo
{
}

// This class will cause InjectModuleInitializer to fail for the same reason.
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class Bar
{
}

This pull request adds the directory of the to-be-modified assembly to Mono.Cecil's search path so that the assembly where those attribute parameters exist can be successfully loaded.

Fixes #8. (I work at the same company as @killyosaur and obtained his code in order to figure out exactly what was happening. It turns out the problem had nothing to do with Visual Studio 2015.)

@bfriesen bfriesen changed the title Moco.Cecil's AssemblyResolver should search target assembly's directory Mono.Cecil's AssemblyResolver should search target assembly's directory Mar 4, 2016
@bfriesen bfriesen force-pushed the assembly_resolver branch from a971306 to f040463 Compare March 4, 2016 05:28
einaregilsson pushed a commit that referenced this pull request Mar 4, 2016
Mono.Cecil's AssemblyResolver should search target assembly's directory
@einaregilsson einaregilsson merged commit 342c0a4 into kzu:master Mar 4, 2016
@einaregilsson
Copy link
Copy Markdown
Contributor

Thanks a lot. Will try to get the package published today. :)

@einaregilsson
Copy link
Copy Markdown
Contributor

Package published!

@bfriesen bfriesen deleted the assembly_resolver branch March 4, 2016 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

possible bug when compiling in 2015 on a project with Newtonsoft.

2 participants