Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamicaly determine whether to use the .NET Framework-specific assembly resolver on .NET Standard. #701

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions Mono.Cecil/BaseAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ public abstract class BaseAssemblyResolver : IAssemblyResolver {

static readonly bool on_mono = Type.GetType ("Mono.Runtime") != null;

static readonly bool on_coreclr = Type.GetType ("System.Runtime.Loader.AssemblyLoadContext") != null;

readonly Collection<string> directories;

#if NET_CORE
// Maps file names of available trusted platform assemblies to their full paths.
// Internal for testing.
internal static readonly Lazy<Dictionary<string, string>> TrustedPlatformAssemblies = new Lazy<Dictionary<string, string>> (CreateTrustedPlatformAssemblyMap);
#else
Collection<string> gac_paths;
#endif
Collection<string> gac_paths;

public void AddSearchDirectory (string directory)
{
Expand Down Expand Up @@ -134,35 +135,39 @@ public virtual AssemblyDefinition Resolve (AssemblyNameReference name, ReaderPar
}

#if NET_CORE
assembly = SearchTrustedPlatformAssemblies (name, parameters);
if (assembly != null)
return assembly;
#else
var framework_dir = Path.GetDirectoryName (typeof (object).Module.FullyQualifiedName);
var framework_dirs = on_mono
? new [] { framework_dir, Path.Combine (framework_dir, "Facades") }
: new [] { framework_dir };
if (on_coreclr) {
assembly = SearchTrustedPlatformAssemblies (name, parameters);
if (assembly != null)
return assembly;
} else
#endif
{
var framework_dir = Path.GetDirectoryName (typeof (object).Module.FullyQualifiedName);
var framework_dirs = on_mono
? new [] { framework_dir, Path.Combine (framework_dir, "Facades") }
: new [] { framework_dir };

if (IsZero (name.Version)) {
assembly = SearchDirectory (name, framework_dirs, parameters);
if (assembly != null)
return assembly;
}

if (IsZero (name.Version)) {
assembly = SearchDirectory (name, framework_dirs, parameters);
if (name.Name == "mscorlib") {
assembly = GetCorlib (name, parameters);
if (assembly != null)
return assembly;
}

assembly = GetAssemblyInGac (name, parameters);
if (assembly != null)
return assembly;
}

if (name.Name == "mscorlib") {
assembly = GetCorlib (name, parameters);
assembly = SearchDirectory (name, framework_dirs, parameters);
if (assembly != null)
return assembly;
}

assembly = GetAssemblyInGac (name, parameters);
if (assembly != null)
return assembly;

assembly = SearchDirectory (name, framework_dirs, parameters);
if (assembly != null)
return assembly;
#endif
if (ResolveFailure != null) {
assembly = ResolveFailure (this, name);
if (assembly != null)
Expand Down Expand Up @@ -231,7 +236,7 @@ static bool IsZero (Version version)
return version.Major == 0 && version.Minor == 0 && version.Build == 0 && version.Revision == 0;
}

#if !NET_CORE
#region .NET Framework-specific resolvers
AssemblyDefinition GetCorlib (AssemblyNameReference reference, ReaderParameters parameters)
{
var version = reference.Version;
Expand Down Expand Up @@ -393,7 +398,7 @@ static string GetAssemblyFile (AssemblyNameReference reference, string prefix, s
Path.Combine (gac, reference.Name), gac_folder.ToString ()),
reference.Name + ".dll");
}
#endif
#endregion
public void Dispose ()
{
Dispose (true);
Expand Down