mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-19 10:12:50 +08:00
38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
namespace Spectre.Console.Cli;
|
|
|
|
internal static class ConfigurationHelper
|
|
{
|
|
public static Type? GetSettingsType(Type commandType)
|
|
{
|
|
if (typeof(ICommand).GetTypeInfo().IsAssignableFrom(commandType) &&
|
|
GetGenericTypeArguments(commandType, typeof(ICommand<>), out var result))
|
|
{
|
|
return result[0];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static bool GetGenericTypeArguments(Type? type, Type genericType,
|
|
[NotNullWhen(true)] out Type[]? genericTypeArguments)
|
|
{
|
|
while (type != null)
|
|
{
|
|
foreach (var @interface in type.GetTypeInfo().GetInterfaces())
|
|
{
|
|
if (!@interface.GetTypeInfo().IsGenericType || @interface.GetGenericTypeDefinition() != genericType)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
genericTypeArguments = @interface.GenericTypeArguments;
|
|
return true;
|
|
}
|
|
|
|
type = type.GetTypeInfo().BaseType;
|
|
}
|
|
|
|
genericTypeArguments = null;
|
|
return false;
|
|
}
|
|
} |