public static ICustomTypeGenerator GetCustomTypeGenerator(Compilation compilation, Type type) { var types = compilation.GetNamespaceTypes(x => x.IsEqualTo <TestCustomTypeGenerator>()); var assembly = AdhocProject.CompileAssembly(types); return((ICustomTypeGenerator)Activator.CreateInstance(assembly.GetTypes().Single(x => x.FullName == type.FullName)) !); }
public void Rewrite() { var project = AdhocProject.FromDirectory($"{TestContext.CurrentContext.TestDirectory}/../../../../AspNetCoreExample.Generator"); project = project.RemoveDocument(project.Documents.Single(x => x.Name == "ApiControllerTypeBuildingContext.cs").Id) .AddDocument("ApiControllerTypeBuildingContext.cs", File.ReadAllText(GetFilePath("ApiControllerTypeBuildingContext.txt"))) .Project; var compilation = project.GetCompilationAsync().GetAwaiter().GetResult() ! .AddReferences(AdhocProject.GetMetadataReferences()) .AddReferences(MetadataReference.CreateFromFile(typeof(ControllerBase).Assembly.Location)); TypeInfoRewriter.Types.Clear(); var tree = compilation.SyntaxTrees.Single(x => x.FilePath.Contains("ApiControllerTypeBuildingContext.cs")); var result = TypeInfoRewriter.Rewrite(compilation, tree); var str = result.ToString(); var expectedCode = File.ReadAllText(GetFilePath("ApiControllerTypeBuildingContext.Expected.txt")).Replace("\r\n", "\n"); str.Diff(expectedCode).ShouldBeEmpty(); var assembly = AdhocProject.CompileAssembly(new[] { result }); var buildingContext = assembly.GetType("AspNetCoreExample.Generator.ApiControllerTypeBuildingContext") !; var acceptMethod = buildingContext.GetMethod("Accept", BindingFlags.Public | BindingFlags.Static) !; acceptMethod.Invoke(null, new object[] { TypeInfo.From <bool>() }).Should().Be(false); acceptMethod.Invoke(null, new object[] { TypeInfo.From <UsersController>() }).Should().Be(true); }