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

#110: Made type lookup more robust. Fixed exception on .NET Standard … #111

Merged
merged 5 commits into from Oct 16, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 29 additions & 24 deletions Common/SystemTypesResolver.cs
@@ -1,3 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using Mono.Cecil;

Expand All @@ -15,18 +17,14 @@ public partial class ModuleWeaver
public void LoadSystemTypes()
{
var mscorlib = AssemblyResolver.Resolve(new AssemblyNameReference("mscorlib", null));
var typeType = mscorlib?.MainModule.Types.FirstOrDefault(x => x.Name == "Type");
if (typeType == null)
{
var runtime = AssemblyResolver.Resolve(new AssemblyNameReference("System.Runtime",null));
typeType = runtime.MainModule.Types.First(x => x.Name == "Type");
}
var funcDefinition = typeType.Module.Types.FirstOrDefault(x => x.Name == "Func`1");
if (funcDefinition == null)
{
var core = AssemblyResolver.Resolve(new AssemblyNameReference("System.Core", null));
funcDefinition = core.MainModule.Types.First(x => x.Name == "Func`1");
}
var netstandard = AssemblyResolver.Resolve(new AssemblyNameReference("netstandard", null));
var runtime = AssemblyResolver.Resolve(new AssemblyNameReference("System.Runtime", null));
var core = AssemblyResolver.Resolve(new AssemblyNameReference("System.Core", null));

var typeType = LoadTypeDefinition("System.Type", mscorlib, netstandard, runtime, core);

var funcDefinition = LoadTypeDefinition("System.Func`1", mscorlib, netstandard, runtime, core);

var genericInstanceType = new GenericInstanceType(funcDefinition);
genericInstanceType.GenericArguments.Add(ModuleDefinition.TypeSystem.String);
GenericFunc = ModuleDefinition.ImportReference(genericInstanceType);
Expand All @@ -41,24 +39,31 @@ public void LoadSystemTypes()
GetTypeFromHandle = ModuleDefinition.ImportReference(GetTypeFromHandle);


var stringType = mscorlib?.MainModule.Types.FirstOrDefault(x => x.Name == "String");
if (stringType == null)
{
var runtime = AssemblyResolver.Resolve(new AssemblyNameReference("System.Runtime", null));
stringType = runtime.MainModule.Types.First(x => x.Name == "String");
}
var stringType = LoadTypeDefinition("System.String", mscorlib, netstandard, runtime, core);

ConcatMethod = ModuleDefinition.ImportReference(stringType.FindMethod("Concat", "String", "String"));
FormatMethod = ModuleDefinition.ImportReference(stringType.FindMethod("Format", "String", "Object[]"));
ObjectArray = new ArrayType(ModuleDefinition.TypeSystem.Object);

var exceptionType = mscorlib?.MainModule.Types.FirstOrDefault(x => x.Name == "Exception");
if (exceptionType == null)
{
var runtime = AssemblyResolver.Resolve(new AssemblyNameReference("System.Runtime",null));
exceptionType = runtime.MainModule.Types.First(x => x.Name == "Exception");
}
var exceptionType = LoadTypeDefinition("System.Exception", mscorlib, netstandard, runtime, core);
ExceptionType = ModuleDefinition.ImportReference(exceptionType);

}

private static TypeDefinition LoadTypeDefinition(
string typeFullName,
params AssemblyDefinition[] canidateAssemblies)
{
foreach (var candidateAssembly in canidateAssemblies)
{
var typeDef = candidateAssembly?.MainModule.Types.FirstOrDefault(x => x.FullName == typeFullName);
if (typeDef != null)
{
Debug.WriteLine("Loaded type {0} from {1}", typeDef.FullName, typeDef.Module.FileName);
return typeDef;
}
}
throw new WeavingException($"Unable to find {typeFullName} among [{String.Join(", ", canidateAssemblies.OfType<AssemblyDefinition>())}]");
}

}