Skip to content

Commit

Permalink
Fix an error in CodeGen that failed to get System.Type (Unity 2021)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Apr 23, 2021
1 parent c9ad649 commit ca981bf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
Expand All @@ -15,6 +16,7 @@ namespace VContainer.Editor.CodeGen
sealed class InjectionILGenerator
{
readonly ModuleDefinition module;
readonly Assembly assembly;
readonly IList<string> targetNamespaces;

TypeReference ObjectResolverTypeRef =>
Expand Down Expand Up @@ -54,9 +56,13 @@ sealed class InjectionILGenerator
MethodReference resolveMethodRef;
MethodReference resolveOrParameterMethodRef;

public InjectionILGenerator(ModuleDefinition module, IList<string> targetNamespaces)
public InjectionILGenerator(
ModuleDefinition module,
Assembly assembly,
IList<string> targetNamespaces)
{
this.module = module;
this.assembly = assembly;
this.targetNamespaces = targetNamespaces;
}

Expand Down Expand Up @@ -110,9 +116,18 @@ public bool TryGenerate(out List<DiagnosticMessage> diagnosticMessages)

bool TryGenerate(TypeDefinition typeDef, List<DiagnosticMessage> diagnosticMessages)
{
var type = Type.GetType($"{typeDef.FullName}, {module.Assembly.FullName}");
var type = assembly.GetType(typeDef.FullName);
if (type == null)
{
diagnosticMessages.Add(new DiagnosticMessage
{
DiagnosticType = DiagnosticType.Warning,
MessageData = $"Skip IL waving because cant detect type: {typeDef.FullName}"
});
return false;
}

if (type == null || !NeedsInjectType(type))
if (!NeedsInjectType(type))
return false;

InjectTypeInfo injectTypeInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Unity.CompilationPipeline.Common.Diagnostics;
using Unity.CompilationPipeline.Common.ILPostProcessing;
using Mono.Cecil;
Expand All @@ -25,8 +26,10 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
if (!WillProcess(compiledAssembly))
return null;

var assembly = Assembly.Load(compiledAssembly.InMemoryAssembly.PeData);

var assemblyDefinition = Utils.LoadAssemblyDefinition(compiledAssembly);
var generator = new InjectionILGenerator(assemblyDefinition.MainModule, null);
var generator = new InjectionILGenerator(assemblyDefinition.MainModule, assembly, null);

if (generator.TryGenerate(out var diagnosticMessages))
{
Expand Down

0 comments on commit ca981bf

Please sign in to comment.