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 1 commit
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
51 changes: 39 additions & 12 deletions Common/SystemTypesResolver.cs
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Linq;
using Mono.Cecil;

Expand All @@ -15,18 +16,33 @@ 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");
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 =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These sets of FirstOrDefault are repeated a few times. Please extract these fragments including the null check and Debug.Write into a method. Maybe something like GetTypeFromFirst(params AssemblyReference[] candidateAssemblies)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, will do

mscorlib?.MainModule.Types.FirstOrDefault(x => x.Name == "Type")
?? runtime?.MainModule.Types.FirstOrDefault(x => x.Name == "Type")
?? netstandard?.MainModule.Types.FirstOrDefault(x => x.Name == "Type")
?? core?.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");
throw new WeavingException("Unable to find System.Type");
}
var funcDefinition = typeType.Module.Types.FirstOrDefault(x => x.Name == "Func`1");
Debug.WriteLine("Loaded type {0} from {1}", typeType.FullName, typeType.Module.FileName);

var funcDefinition =
mscorlib?.MainModule.Types.FirstOrDefault(x => x.Name == "Func`1")
?? runtime?.MainModule.Types.FirstOrDefault(x => x.Name == "Func`1")
?? netstandard?.MainModule.Types.FirstOrDefault(x => x.Name == "Func`1")
?? core?.MainModule.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");
throw new WeavingException("Unable to find System.Func`1");
}
Debug.WriteLine("Loaded type {0} from {1}", funcDefinition.FullName, funcDefinition.Module.FileName);

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


var stringType = mscorlib?.MainModule.Types.FirstOrDefault(x => x.Name == "String");
var stringType =
mscorlib?.MainModule.Types.FirstOrDefault(x => x.Name == "String")
?? runtime?.MainModule.Types.FirstOrDefault(x => x.Name == "String")
?? netstandard?.MainModule.Types.FirstOrDefault(x => x.Name == "String")
?? core?.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");
throw new WeavingException("Unable to find System.String");
}
Debug.WriteLine("Loaded type {0} from {1}", stringType.FullName, stringType.Module.FileName);

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");
var exceptionType =
mscorlib?.MainModule.Types.FirstOrDefault(x => x.Name == "Exception")
?? runtime?.MainModule.Types.FirstOrDefault(x => x.Name == "Exception")
?? netstandard?.MainModule.Types.FirstOrDefault(x => x.Name == "Exception")
?? core?.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");
throw new WeavingException("Unable to find System.Exception");
}
Debug.WriteLine("Loaded type {0} from {1}", exceptionType.FullName, exceptionType.Module.FileName);

ExceptionType = ModuleDefinition.ImportReference(exceptionType);

}
Expand Down