From fa12db3e287415bbefd23ea88672993d158f1704 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Mon, 29 Jan 2024 10:04:10 +0300 Subject: [PATCH] Remove AttributeFinder --- .../AttributedDestructuringPolicy.cs | 12 +++++---- .../Util/AttributeFinder.cs | 26 ------------------- .../Util/CacheEntry.cs | 2 +- 3 files changed, 8 insertions(+), 32 deletions(-) delete mode 100644 src/Destructurama.Attributed/Util/AttributeFinder.cs diff --git a/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs b/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs index bbb8d74..6dbebc6 100644 --- a/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs +++ b/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs @@ -68,25 +68,27 @@ private static IEnumerable GetPropertiesRecursive(Type type) private CacheEntry CreateCacheEntry(Type type) { - var classDestructurer = type.GetCustomAttribute(); + static T GetCustomAttribute(PropertyInfo propertyInfo) => propertyInfo.GetCustomAttributes().OfType().FirstOrDefault(); + + var classDestructurer = type.GetCustomAttributes().OfType().FirstOrDefault(); if (classDestructurer != null) return new(classDestructurer.CreateLogEventPropertyValue); var properties = GetPropertiesRecursive(type).ToList(); if (!_options.IgnoreNullProperties && properties.All(pi => - pi.GetCustomAttribute() == null - && pi.GetCustomAttribute() == null)) + GetCustomAttribute(pi) == null + && GetCustomAttribute(pi) == null)) { return CacheEntry.Ignore; } var optionalIgnoreAttributes = properties - .Select(pi => new { pi, Attribute = pi.GetCustomAttribute() }) + .Select(pi => new { pi, Attribute = GetCustomAttribute(pi) }) .Where(o => o.Attribute != null) .ToDictionary(o => o.pi, o => o.Attribute); var destructuringAttributes = properties - .Select(pi => new { pi, Attribute = pi.GetCustomAttribute() }) + .Select(pi => new { pi, Attribute = GetCustomAttribute(pi) }) .Where(o => o.Attribute != null) .ToDictionary(o => o.pi, o => o.Attribute); diff --git a/src/Destructurama.Attributed/Util/AttributeFinder.cs b/src/Destructurama.Attributed/Util/AttributeFinder.cs deleted file mode 100644 index 6b5cc29..0000000 --- a/src/Destructurama.Attributed/Util/AttributeFinder.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2018 Destructurama Contributors, Serilog Contributors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using System.Reflection; - -namespace Destructurama.Util; - -internal static class AttributeFinder -{ - public static T GetCustomAttribute(this Type typeInfo) => - typeInfo.GetCustomAttributes().OfType().FirstOrDefault(); - - public static T GetCustomAttribute(this PropertyInfo propertyInfo) => - propertyInfo.GetCustomAttributes().OfType().FirstOrDefault(); -} diff --git a/src/Destructurama.Attributed/Util/CacheEntry.cs b/src/Destructurama.Attributed/Util/CacheEntry.cs index b1033b1..114df3b 100644 --- a/src/Destructurama.Attributed/Util/CacheEntry.cs +++ b/src/Destructurama.Attributed/Util/CacheEntry.cs @@ -17,7 +17,7 @@ namespace Destructurama.Util; -internal struct CacheEntry +internal readonly struct CacheEntry { public CacheEntry(Func destructureFunc) {