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

Suggestion: Use generic types for functions provided in SymbolsFunctions (and others) #122

Open
blahDL opened this issue Aug 5, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@blahDL
Copy link

blahDL commented Aug 5, 2024

When making use of the functions provided by classes such as SymbolsFunctions in C# code we have to cast the type back to the original type to access additional properties on the objects.

// to get the list of all parameters that have the `FromRoute` attribute across all controllers you have to do something like this
 classes
    .ThatInheritFrom("ControllerBase")
    .Cast<IClass>() // ThatInheritFrom returns a collection of ISymbolBase so we need to cast it back to IClass
    .SelectMany(controller => controller.Methods)
    .SelectMany(method => method.Parameters.ThatHaveAttribute("FromRoute"))
    .Cast<IParameter>() // ThatHaveAttribute returns a collection of ISymbolBase so we need to cast it back to IParameter

This should be relatively simple and low risk to implement. For example the following function WhereNamespaceStartsWith

public static IEnumerable<ISymbolBase> WhereNamespaceStartsWith(this IEnumerable<ISymbolBase> symbols, string prefix)
{
    var result = symbols.Where(x => x.Namespace.StartsWith(prefix));
    return result;
}

Could be updated as follows

public static IEnumerable<T> WhereNamespaceStartsWith<T>(this IEnumerable<T> symbols, string prefix) where T: ISymbolBase
{
    var result = symbols.Where(x => x.Namespace.StartsWith(prefix));
    return result;
}

If this was updated for all functions provided the example at the start of this issue would no longer need to cast the type back to the original type so it would be as follows

// to get the list of all parameters that have the `FromRoute` attribute across all controllers you can do this
 classes
    .ThatInheritFrom("ControllerBase")
    .SelectMany(controller => controller.Methods)
    .SelectMany(method => method.Parameters.ThatHaveAttribute("FromRoute"))
@NeVeSpl NeVeSpl added the enhancement New feature or request label Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants