Skip to content

Refactor generator Abstractions symbol names to centralized nameof-based constants#23

Merged
dex3r merged 2 commits into
mainfrom
copilot/refactor-generators-use-nameof
Feb 28, 2026
Merged

Refactor generator Abstractions symbol names to centralized nameof-based constants#23
dex3r merged 2 commits into
mainfrom
copilot/refactor-generators-use-nameof

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

Generators currently mix nameof-based symbol references with hard-coded Abstractions type/member strings (e.g. fully-qualified type names used in reflection). This change removes copied string literals for Abstractions symbols and standardizes them via Consts.

  • What changed

    • Added Abstractions-related reflection constants to MattSourceGenHelpers.Generators/Consts.cs, derived from nameof(...):
      • assembly name
      • Generate full type name
      • RecordingGeneratorsFactory full type name
      • Generate.CurrentGenerator property name
      • RecordingGeneratorsFactory.LastRecord property name
    • Updated GeneratesMethodExecutionRuntime to consume these constants instead of local hard-coded strings.
  • Why this matters

    • Keeps generator-side Abstractions symbol references consistent and refactor-safe.
    • Consolidates symbol naming in one place (Consts) rather than duplicating string literals in runtime code.
  • Example

    public const string GenerateTypeFullName = $"{AbstractionsNamespace}.{nameof(Generate)}";
    public const string CurrentGeneratorPropertyName = nameof(Generate.CurrentGenerator);
    Type? generatorStaticType = abstractionsAssembly.GetType(Consts.GenerateTypeFullName);
    PropertyInfo? currentGeneratorProperty =
        generatorStaticType.GetProperty(Consts.CurrentGeneratorPropertyName, BindingFlags.Public | BindingFlags.Static);
Original prompt

This section details on the original issue you should resolve

<issue_title>In Generators, refactor string to use nameof instead</issue_title>
<issue_description>In Generators, sometimes code from Abstraction is reference as a string, like

private const string GenerateTypeName = "MattSourceGenHelpers.Abstractions.Generate";

But other times, a Consts class is used with nameof.

Replace all copied names in strings from Abstraction in the Generators class to use nameof instead, moved to consts class where it makes sense.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor Generators to use nameof instead of strings Refactor generator Abstractions symbol names to centralized nameof-based constants Feb 28, 2026
@dex3r dex3r marked this pull request as ready for review February 28, 2026 22:19
Copilot AI review requested due to automatic review settings February 28, 2026 22:19
@dex3r dex3r merged commit 8f6d90e into main Feb 28, 2026
1 check passed
@dex3r dex3r deleted the copilot/refactor-generators-use-nameof branch February 28, 2026 22:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the MattSourceGenHelpers.Generators project to eliminate hard-coded Abstractions type/member string literals in GeneratesMethodExecutionRuntime.cs, replacing them with centralized nameof-based constants defined in Consts.cs. This makes the generator's references to Abstractions symbols refactor-safe and consistent with how other symbol names were already defined in Consts.

Changes:

  • Added five new constants to Consts.cs: AbstractionsAssemblyName, GenerateTypeFullName, RecordingGeneratorsFactoryTypeFullName, CurrentGeneratorPropertyName, and LastRecordPropertyName.
  • Removed five private const string declarations from GeneratesMethodExecutionRuntime.cs.
  • Updated all usages in GeneratesMethodExecutionRuntime.cs to reference the new Consts properties.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
MattSourceGenHelpers.Generators/Consts.cs Adds Abstractions assembly name, type full names, and property name constants derived from nameof
MattSourceGenHelpers.Generators/GeneratesMethodExecutionRuntime.cs Removes local private const strings and replaces all usages with Consts.* references

One issue was identified:

AbstractionsAssemblyName on line 10 of Consts.cs is defined as a direct alias of AbstractionsNamespace. While currently correct (both resolve to "MattSourceGenHelpers.Abstractions"), these are semantically distinct concepts — an assembly name and a CLR namespace. If the assembly name were ever overridden in the .csproj (via <AssemblyName>) without a corresponding namespace change (or vice versa), this constant would silently produce the wrong value and assembly lookup in ExecuteFluentGeneratorMethod would break. A dedicated, independent constant (e.g., using typeof(Generate).Assembly.GetName().Name at runtime, or a standalone literal) would be more robust.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

public static class Consts
{
public const string AbstractionsNamespace = $"{nameof(MattSourceGenHelpers)}.{nameof(Abstractions)}";
public const string AbstractionsAssemblyName = AbstractionsNamespace;
Copy link

Copilot AI Feb 28, 2026

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In Generators, refactor string to use nameof instead

3 participants