Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion MattSourceGenHelpers.Generators/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ namespace MattSourceGenHelpers.Generators;
public static class Consts
{
public const string AbstractionsNamespace = $"{nameof(MattSourceGenHelpers)}.{nameof(Abstractions)}";
public const string AbstractionsAssemblyName = AbstractionsNamespace;

Copilot AI Feb 28, 2026

Copy link

Choose a reason for hiding this comment

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

AbstractionsAssemblyName is defined as a simple alias of AbstractionsNamespace. While these values happen to be identical today ("MattSourceGenHelpers.Abstractions"), they represent two conceptually different things: a namespace and an assembly name. Assembly names and namespaces can diverge independently (e.g., if the project's <AssemblyName> is overridden without changing the namespace, or vice versa). A more robust definition would derive AbstractionsAssemblyName independently from an <AssemblyName> expression or a nameof-based approach that directly references the assembly identity rather than reusing the namespace constant.

Suggested change
public const string AbstractionsAssemblyName = AbstractionsNamespace;
public static readonly string AbstractionsAssemblyName = typeof(Generate).Assembly.GetName().Name;

Copilot uses AI. Check for mistakes.
public const string SwitchCaseAttributeFullName = $"{AbstractionsNamespace}.{nameof(SwitchCase)}";
public const string SwitchDefaultAttributeFullName = $"{AbstractionsNamespace}.{nameof(SwitchDefault)}";
public const string GeneratesMethodAttributeFullName = $"{AbstractionsNamespace}.{nameof(GeneratesMethod)}";
public const string IMethodImplementationGeneratorFullName = $"{AbstractionsNamespace}.{nameof(IMethodImplementationGenerator)}";
}
public const string GenerateTypeFullName = $"{AbstractionsNamespace}.{nameof(Generate)}";
public const string RecordingGeneratorsFactoryTypeFullName = $"{AbstractionsNamespace}.{nameof(RecordingGeneratorsFactory)}";
public const string CurrentGeneratorPropertyName = nameof(Generate.CurrentGenerator);
public const string LastRecordPropertyName = nameof(RecordingGeneratorsFactory.LastRecord);
}
24 changes: 9 additions & 15 deletions MattSourceGenHelpers.Generators/GeneratesMethodExecutionRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ internal sealed record SwitchBodyData(

internal static class GeneratesMethodExecutionRuntime
{
private const string AbstractionsAssemblyName = "MattSourceGenHelpers.Abstractions";
private const string GenerateTypeName = "MattSourceGenHelpers.Abstractions.Generate";
private const string RecordingGeneratorsFactoryTypeName = "MattSourceGenHelpers.Abstractions.RecordingGeneratorsFactory";
private const string CurrentGeneratorPropertyName = "CurrentGenerator";
private const string LastRecordPropertyName = "LastRecord";

internal static (string? value, string? error) ExecuteSimpleGeneratorMethod(
IMethodSymbol generatorMethod,
IMethodSymbol partialMethod,
Expand Down Expand Up @@ -71,27 +65,27 @@ internal static (SwitchBodyData? record, string? error) ExecuteFluentGeneratorMe
PortableExecutableReference? abstractionsReference = compilation.References
.OfType<PortableExecutableReference>()
.FirstOrDefault(reference => reference.FilePath is not null && string.Equals(
Path.GetFileNameWithoutExtension(reference.FilePath),
AbstractionsAssemblyName,
StringComparison.OrdinalIgnoreCase));
Path.GetFileNameWithoutExtension(reference.FilePath),
Consts.AbstractionsAssemblyName,
StringComparison.OrdinalIgnoreCase));

if (abstractionsReference?.FilePath == null)
{
return (null, $"Could not find {AbstractionsAssemblyName} reference in compilation");
return (null, $"Could not find {Consts.AbstractionsAssemblyName} reference in compilation");
}

string abstractionsAssemblyPath = ResolveImplementationAssemblyPath(abstractionsReference.FilePath);
Assembly abstractionsAssembly = loadContext.LoadFromAssemblyPath(abstractionsAssemblyPath);

Type? generatorStaticType = abstractionsAssembly.GetType(GenerateTypeName);
Type? recordingFactoryType = abstractionsAssembly.GetType(RecordingGeneratorsFactoryTypeName);
Type? generatorStaticType = abstractionsAssembly.GetType(Consts.GenerateTypeFullName);
Type? recordingFactoryType = abstractionsAssembly.GetType(Consts.RecordingGeneratorsFactoryTypeFullName);
if (generatorStaticType == null || recordingFactoryType == null)
{
return (null, $"Could not find {GenerateTypeName} or {RecordingGeneratorsFactoryTypeName} types in Abstractions assembly");
return (null, $"Could not find {Consts.GenerateTypeFullName} or {Consts.RecordingGeneratorsFactoryTypeFullName} types in Abstractions assembly");
}

object? recordingFactory = Activator.CreateInstance(recordingFactoryType);
PropertyInfo? currentGeneratorProperty = generatorStaticType.GetProperty(CurrentGeneratorPropertyName, BindingFlags.Public | BindingFlags.Static);
PropertyInfo? currentGeneratorProperty = generatorStaticType.GetProperty(Consts.CurrentGeneratorPropertyName, BindingFlags.Public | BindingFlags.Static);
currentGeneratorProperty?.SetValue(null, recordingFactory);

string typeName = generatorMethod.ContainingType.ToDisplayString();
Expand All @@ -109,7 +103,7 @@ internal static (SwitchBodyData? record, string? error) ExecuteFluentGeneratorMe

generatorMethodInfo.Invoke(null, null);

PropertyInfo? lastRecordProperty = recordingFactoryType.GetProperty(LastRecordPropertyName);
PropertyInfo? lastRecordProperty = recordingFactoryType.GetProperty(Consts.LastRecordPropertyName);
object? lastRecord = lastRecordProperty?.GetValue(recordingFactory);
if (lastRecord == null)
{
Expand Down