Skip to content

Commit

Permalink
Query the public members only once. This will simplify the code and i…
Browse files Browse the repository at this point in the history
…mprove performance a littlebit.
  • Loading branch information
Colin Wilmans committed Sep 3, 2024
1 parent f30a718 commit c400ee3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<Version>5.0.1</Version>
<Version>5.0.2</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
<PackageReleaseNotes>Use ForAttributeWithMetadataName to improve performance</PackageReleaseNotes>
<PackageReleaseNotes>Query members only once. Small optimization.</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugGenerator|AnyCPU'">
Expand Down
44 changes: 17 additions & 27 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ is not ClassDeclarationSyntax classSyntax

interfaceGenerator.AddClassDocumentation(GetDocumentationForClass(classSyntax));
interfaceGenerator.AddGeneric(GetGeneric(classSyntax));
AddPropertiesToInterface(typeSymbol, interfaceGenerator);
AddMethodsToInterface(typeSymbol, interfaceGenerator);
AddEventsToInterface(typeSymbol, interfaceGenerator);

var members = typeSymbol
.GetAllMembers()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsStatic)
.Where(x => !HasIgnoreAttribute(x))
.ToList();

AddPropertiesToInterface(members, interfaceGenerator);
AddMethodsToInterface(members, interfaceGenerator);
AddEventsToInterface(members, interfaceGenerator);

var generatedCode = interfaceGenerator.Build();

Expand All @@ -74,19 +82,12 @@ private static string GetNameSpace(ISymbol typeSymbol)
: customNs!;
}

private static void AddMethodsToInterface(
ITypeSymbol classSymbol,
InterfaceBuilder codeGenerator
)
private static void AddMethodsToInterface(List<ISymbol> members, InterfaceBuilder codeGenerator)
{
classSymbol
.GetAllMembers()
members
.Where(x => x.Kind == SymbolKind.Method)
.OfType<IMethodSymbol>()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => x.MethodKind == MethodKind.Ordinary)
.Where(x => !x.IsStatic)
.Where(x => !HasIgnoreAttribute(x))
.Where(x => x.ContainingType.Name != nameof(Object))
.Where(x => !HasIgnoreAttribute(x))
.GroupBy(x => x.ToDisplayString(MethodSignatureDisplayFormat))
Expand Down Expand Up @@ -172,18 +173,11 @@ private static bool IsNullable(ITypeSymbol typeSymbol)
return false;
}

private static void AddEventsToInterface(
ITypeSymbol classSymbol,
InterfaceBuilder codeGenerator
)
private static void AddEventsToInterface(List<ISymbol> members, InterfaceBuilder codeGenerator)
{
classSymbol
.GetAllMembers()
members
.Where(x => x.Kind == SymbolKind.Event)
.OfType<IEventSymbol>()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsStatic)
.Where(x => !HasIgnoreAttribute(x))
.GroupBy(x => x.ToDisplayString(MethodSignatureDisplayFormat))
.Select(g => g.First())
.ToList()
Expand Down Expand Up @@ -253,18 +247,14 @@ private static string GetRefKind(IParameterSymbol x)
}

private static void AddPropertiesToInterface(
ITypeSymbol classSymbol,
List<ISymbol> members,
InterfaceBuilder interfaceGenerator
)
{
classSymbol
.GetAllMembers()
members
.Where(x => x.Kind == SymbolKind.Property)
.OfType<IPropertySymbol>()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsStatic)
.Where(x => !x.IsIndexer)
.Where(x => !HasIgnoreAttribute(x))
.GroupBy(x => x.Name)
.Select(g => g.First())
.ToList()
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ Should be simply a build and run Tests

## Changelog

### 5.0.0
### 5.0.2

- Query members only once. Small optimization. Thanks, @crwsolutions!

### 5.0.1

- Sync DemoClass and actual generated code with README. Thanks, @crwsolutions!
- Removed manual attribute in TestNuget project. Thanks, @crwsolutions!
- Removed manual attribute in TestNuget project. Thanks, @crwsolutions!

### 5.0.0

Expand Down

0 comments on commit c400ee3

Please sign in to comment.