From c400ee382c0c5b204ccd9f20311613bb503505a7 Mon Sep 17 00:00:00 2001 From: Colin Wilmans Date: Tue, 3 Sep 2024 16:59:12 +0200 Subject: [PATCH] Query the public members only once. This will simplify the code and improve performance a littlebit. --- .../AutomaticInterface.csproj | 4 +- .../AutomaticInterface/Builder.cs | 44 +++++++------------ README.md | 8 +++- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/AutomaticInterface/AutomaticInterface/AutomaticInterface.csproj b/AutomaticInterface/AutomaticInterface/AutomaticInterface.csproj index ad50e30..a41e7c1 100644 --- a/AutomaticInterface/AutomaticInterface/AutomaticInterface.csproj +++ b/AutomaticInterface/AutomaticInterface/AutomaticInterface.csproj @@ -25,11 +25,11 @@ MIT True latest-Recommended - 5.0.1 + 5.0.2 README.md true 1701;1702;NU5128 - Use ForAttributeWithMetadataName to improve performance + Query members only once. Small optimization. diff --git a/AutomaticInterface/AutomaticInterface/Builder.cs b/AutomaticInterface/AutomaticInterface/Builder.cs index a75cd87..c1ff967 100644 --- a/AutomaticInterface/AutomaticInterface/Builder.cs +++ b/AutomaticInterface/AutomaticInterface/Builder.cs @@ -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(); @@ -74,19 +82,12 @@ private static string GetNameSpace(ISymbol typeSymbol) : customNs!; } - private static void AddMethodsToInterface( - ITypeSymbol classSymbol, - InterfaceBuilder codeGenerator - ) + private static void AddMethodsToInterface(List members, InterfaceBuilder codeGenerator) { - classSymbol - .GetAllMembers() + members .Where(x => x.Kind == SymbolKind.Method) .OfType() - .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)) @@ -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 members, InterfaceBuilder codeGenerator) { - classSymbol - .GetAllMembers() + members .Where(x => x.Kind == SymbolKind.Event) .OfType() - .Where(x => x.DeclaredAccessibility == Accessibility.Public) - .Where(x => !x.IsStatic) - .Where(x => !HasIgnoreAttribute(x)) .GroupBy(x => x.ToDisplayString(MethodSignatureDisplayFormat)) .Select(g => g.First()) .ToList() @@ -253,18 +247,14 @@ private static string GetRefKind(IParameterSymbol x) } private static void AddPropertiesToInterface( - ITypeSymbol classSymbol, + List members, InterfaceBuilder interfaceGenerator ) { - classSymbol - .GetAllMembers() + members .Where(x => x.Kind == SymbolKind.Property) .OfType() - .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() diff --git a/README.md b/README.md index 57e9758..639fb91 100644 --- a/README.md +++ b/README.md @@ -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