Clean up ReflectionCache source generator

This commit is contained in:
YoshiRulz 2021-11-28 03:22:20 +10:00
parent eec1711b0f
commit 182699b967
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 4 additions and 6 deletions

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -62,27 +61,26 @@ namespace BizHawk.SrcGen.ReflectionCache
{ {
if (context.SyntaxReceiver is not ReflectionCacheGenSyntaxReceiver receiver) return; if (context.SyntaxReceiver is not ReflectionCacheGenSyntaxReceiver receiver) return;
var nSpace = receiver.Namespace; var nSpace = receiver.Namespace;
if (nSpace == null) return;
var extraImports = nSpace == "BizHawk.Common" ? string.Empty : "\nusing BizHawk.Common;";
var src = $@"#nullable enable var src = $@"#nullable enable
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
{extraImports} {(nSpace == "BizHawk.Common" ? string.Empty : "\nusing BizHawk.Common;")}
namespace {nSpace} namespace {nSpace}
{{ {{
public static class ReflectionCache public static class ReflectionCache
{{ {{
private static Type[]? _types = null;
private static readonly Assembly Asm = typeof({nSpace}.ReflectionCache).Assembly; private static readonly Assembly Asm = typeof({nSpace}.ReflectionCache).Assembly;
public static readonly Version AsmVersion = Asm.GetName().Version!; public static readonly Version AsmVersion = Asm.GetName().Version!;
private static readonly Lazy<Type[]> _types = new Lazy<Type[]>(() => Asm.GetTypesWithoutLoadErrors().ToArray()); public static Type[] Types => _types ??= Asm.GetTypesWithoutLoadErrors().ToArray();
public static Type[] Types => _types.Value;
/// <exception cref=""ArgumentException"">not found</exception> /// <exception cref=""ArgumentException"">not found</exception>
public static Stream EmbeddedResourceStream(string embedPath) public static Stream EmbeddedResourceStream(string embedPath)