Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude Attribute-Inheriting Classes from Source Generation #502

Closed
chikacc opened this issue Apr 14, 2023 · 1 comment
Closed

Exclude Attribute-Inheriting Classes from Source Generation #502

chikacc opened this issue Apr 14, 2023 · 1 comment

Comments

@chikacc
Copy link
Contributor

chikacc commented Apr 14, 2023

Please describe the feature you would like to see implemented:

I would like to request a feature to exclude classes that inherit from Attribute in the Source Generator. This feature would allow users to avoid unnecessary code generation for classes that are not intended for dependency injection.

Describe the solution you'd like:

Modify the Source Generator to include a check for classes that inherit from Attribute and exclude them from the generation process. This can be achieved by iterating through the classes and checking their inheritance chain to identify those that inherit from Attribute.

Example:

foreach (var classSymbol in compilation.GetSymbols())
{
    if (!InheritsFromAttribute(classSymbol))
    {
        // Proceed with code generation
    }
}

bool InheritsFromAttribute(INamedTypeSymbol classSymbol)
{
    while (classSymbol != null)
    {
        if (classSymbol.Name == nameof(Attribute))
        {
            return true;
        }
        classSymbol = classSymbol.BaseType;
    }
    return false;
}

Describe alternatives you've considered:

An alternative solution could involve using an attribute (e.g., [VContainerExclude]) on specific classes to manually exclude them from the code generation process. However, this approach requires additional manual work from users and may not be as efficient as automatically excluding classes inheriting from Attribute.

@hadashiA
Copy link
Owner

hadashiA commented May 3, 2023

@chikacc Thanks for the suggestion.

In #510, a change was made to exclude Attribute.

You can also exclude a class by adding [InjectIgnore] to the class definition.
This was an undocumented feature, but is now documented in #511.

@hadashiA hadashiA closed this as completed May 3, 2023
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

No branches or pull requests

2 participants