From adf2b77d172f4b347e05b5c98937deea278f4a9a Mon Sep 17 00:00:00 2001 From: Daniele Corsini Date: Tue, 16 Jul 2024 22:59:29 +0200 Subject: [PATCH] [Docs] Improve API Documentation performance (#2377) * Improve documentation * Add space --------- Co-authored-by: Vincent Baaij --- .../Shared/Components/ApiDocumentation.razor | 2 +- .../Components/ApiDocumentation.razor.cs | 56 +++++++++++-------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/examples/Demo/Shared/Components/ApiDocumentation.razor b/examples/Demo/Shared/Components/ApiDocumentation.razor index cffacb48ed..f27addbad0 100644 --- a/examples/Demo/Shared/Components/ApiDocumentation.razor +++ b/examples/Demo/Shared/Components/ApiDocumentation.razor @@ -53,7 +53,7 @@ { var id = Identifier.NewId(); -
Preview:
+
Preview: 
@context.Default diff --git a/examples/Demo/Shared/Components/ApiDocumentation.razor.cs b/examples/Demo/Shared/Components/ApiDocumentation.razor.cs index 194dacbafb..156e525ebf 100644 --- a/examples/Demo/Shared/Components/ApiDocumentation.razor.cs +++ b/examples/Demo/Shared/Components/ApiDocumentation.razor.cs @@ -13,8 +13,8 @@ private class MemberDescription public MemberTypes MemberType { get; set; } = MemberTypes.Property; public string Name { get; set; } = ""; public string Type { get; set; } = ""; - public string[] EnumValues { get; set; } = Array.Empty(); - public string[] Parameters { get; set; } = Array.Empty(); + public string[] EnumValues { get; set; } = []; + public string[] Parameters { get; set; } = []; public string? Default { get; set; } = null; public string Description { get; set; } = ""; public bool IsParameter { get; set; } @@ -39,7 +39,7 @@ private class MemberDescription /// Default for this parameter is 'typeof(string)' /// [Parameter] - public Type[] InstanceTypes { get; set; } = new[] { typeof(string) }; + public Type[] InstanceTypes { get; set; } = [typeof(string)]; /// /// Gets or sets the label used for displaying the type parameter. @@ -70,29 +70,37 @@ private IEnumerable GetMembers(MemberTypes type) { List? members = []; - object? obj; - if (Component.IsGenericType) + object? obj = null; + var created = false; + + object? GetObjectValue(string propertyName) { - if (InstanceTypes is null) + if (!created) { - throw new ArgumentNullException(nameof(InstanceTypes), "InstanceTypes must be specified when Component is a generic type"); - } + if (Component.IsGenericType) + { + if (InstanceTypes is null) + { + throw new ArgumentNullException(nameof(InstanceTypes), "InstanceTypes must be specified when Component is a generic type"); + } - // Supply the type to create the generic instance with (needs to be an array) - Type[] typeArgs = InstanceTypes; - Type constructed = Component.MakeGenericType(typeArgs); + // Supply the type to create the generic instance with (needs to be an array) + obj = Activator.CreateInstance(Component.MakeGenericType(InstanceTypes)); + } + else + { + obj = Activator.CreateInstance(Component); + } + created = true; + } - obj = Activator.CreateInstance(constructed); - } - else - { - obj = Activator.CreateInstance(Component); - } + return obj?.GetType().GetProperty(propertyName)?.GetValue(obj); + }; - IEnumerable? allProperties = Component.GetProperties().Select(i => (MemberInfo)i); - IEnumerable? allMethods = Component.GetMethods().Where(i => !i.IsSpecialName).Select(i => (MemberInfo)i); + var allProperties = Component.GetProperties().Select(i => (MemberInfo)i); + var allMethods = Component.GetMethods().Where(i => !i.IsSpecialName).Select(i => (MemberInfo)i); - foreach (MemberInfo memberInfo in allProperties.Union(allMethods).OrderBy(m => m.Name)) + foreach (var memberInfo in allProperties.Union(allMethods).OrderBy(m => m.Name)) { try { @@ -105,7 +113,7 @@ private IEnumerable GetMembers(MemberTypes type) { var isParameter = memberInfo.GetCustomAttribute() != null; - Type t = propertyInfo.PropertyType; + var t = propertyInfo.PropertyType; var isEvent = t == typeof(EventCallback) || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(EventCallback<>)); // Parameters/properties @@ -115,11 +123,11 @@ private IEnumerable GetMembers(MemberTypes type) var defaultVaue = ""; if (propertyInfo.PropertyType.IsValueType || propertyInfo.PropertyType == typeof(string)) { - defaultVaue = obj?.GetType().GetProperty(propertyInfo.Name)?.GetValue(obj)?.ToString(); + defaultVaue = GetObjectValue(propertyInfo.Name)?.ToString(); } else if (propertyInfo.PropertyType == typeof(Icon)) { - if (obj?.GetType().GetProperty(propertyInfo.Name)?.GetValue(obj) is Icon value) + if (GetObjectValue(propertyInfo.Name) is Icon value) { icon = value; defaultVaue = $"{value.Variant}.{value.Size}.{value.Name}"; @@ -201,6 +209,6 @@ private static string[] GetEnumValues(PropertyInfo? propertyInfo) } } - return Array.Empty(); + return []; } }