Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/Gemstone.Timeseries/Adapters/AdapterCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using System.Text.Json.Serialization;
using System.Threading;
using Gemstone.ComponentModel.DataAnnotations;
using Gemstone.Diagnostics;
using Gemstone.EventHandlerExtensions;
using Gemstone.StringExtensions;
using Gemstone.TypeExtensions;
Expand Down Expand Up @@ -446,10 +447,10 @@
}

/// <summary>
/// Gets the AdapterProtocol attribute for the protocol acronym.
/// Gets the <see cref="AdapterProtocolAttribute"/> for the protocol acronym.
/// </summary>
/// <param name="acronym"></param>
/// <returns>Adapter Protocl for the protocol acronym.</returns>
/// <returns>Adapter protocol attribute for the protocol acronym.</returns>
public static AdapterProtocolAttribute? GetProtocolAttribute(string acronym)
{
return AdapterProtocols.Values
Expand Down Expand Up @@ -568,23 +569,49 @@
internal static IEnumerable<(AdapterInfo info, TAttr[] attributes)> GetAdapterAttributes<TAttr>(this IEnumerable<AdapterInfo> adapters) where TAttr : Attribute
{
return adapters
.Select(info => (info, attributes: info.Type.GetCustomAttributes<TAttr>().ToArray()))
.Select(info => (info, attributes: safeGetCustomAttributes(info.Type)))
.Where(item => item.attributes.Length > 0);

static TAttr[] safeGetCustomAttributes(Type type)
{
try
{
return type.GetCustomAttributes<TAttr>().ToArray();
}
catch (Exception ex)
{
Logger.SwallowException(ex, $"Failed to get custom attributes for type '{typeof(TAttr).FullName}'", $"{nameof(AdapterCache)}.{nameof(GetAdapterAttributes)}");
return [];
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
Comment thread
ritchiecarroll marked this conversation as resolved.
Dismissed
}
}

// Gets all adapters grouped with each of its specified attributes for all adapters with methods that are marked with the attribute.
internal static IEnumerable<(AdapterInfo info, (MethodInfo method, TAttr attribute)[] methodAttributes)> GetAdapterMethodAttributes<TAttr>(this IEnumerable<AdapterInfo> adapters) where TAttr : Attribute
{
return adapters.Select(info => (info, methodAttributes: getMethodAttributes(info)));

(MethodInfo, TAttr)[] getMethodAttributes(AdapterInfo info)
static (MethodInfo, TAttr)[] getMethodAttributes(AdapterInfo info)
{
return info.Type
.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase)
.Select(method => (method, attribute: method.GetCustomAttribute<TAttr>()))
.Select(method => (method, attribute: safeGetCustomMethodAttr(method)))
.Where(item => item.attribute is not null)
.ToArray()!;
}

static TAttr? safeGetCustomMethodAttr(MethodInfo method)
{
try
{
return method.GetCustomAttribute<TAttr>();
}
catch (Exception ex)
{
Logger.SwallowException(ex, $"Failed to get custom attribute for method '{method.Name}' of type '{method.DeclaringType?.FullName}'", $"{nameof(AdapterCache)}.{nameof(GetAdapterMethodAttributes)}");
return null;
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
Comment thread
ritchiecarroll marked this conversation as resolved.
Dismissed
}
}

// Gets all adapters filtered by editor browsable state.
Expand Down
Loading