Skip to content

Commit

Permalink
Migration to modern language features
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jul 12, 2023
1 parent da1d799 commit 5a0ba33
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
5 changes: 2 additions & 3 deletions src/DotNext/Reflection/CollectionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ public static class CollectionType
{
foreach (var collectionType in (typeof(IReadOnlyCollection<>), typeof(ICollection<>)).AsReadOnlySpan())
{
var instance = type.FindGenericInstance(collectionType);
if (instance is not null)
return instance;
if (type.FindGenericInstance(collectionType) is { } result)
return result;
}

return null;
Expand Down
48 changes: 33 additions & 15 deletions src/DotNext/Reflection/TaskType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ public static Type MakeTaskType(this Type taskResult, bool valueTask)
return (valueTask ? typeof(ValueTask<>) : typeof(Task<>)).MakeGenericType(taskResult);
}

private static Type? GetValueTaskType(Type valueTaskType)
{
if (valueTaskType == typeof(ValueTask))
return typeof(void);

if (valueTaskType.IsConstructedGenericType && valueTaskType.GetGenericTypeDefinition() == typeof(ValueTask<>))
return valueTaskType.GetGenericArguments()[0];

return null;
}

/// <summary>
/// Obtains result type from task type.
/// </summary>
Expand All @@ -97,11 +86,40 @@ public static Type MakeTaskType(this Type taskResult, bool valueTask)
if (taskType == CompletedTaskType)
return typeof(void);

var result = taskType.FindGenericInstance(typeof(Task<>));
if (result is not null)
return result.GetGenericArguments()[0];
if (taskType.FindGenericInstance(typeof(Task<>)) is { } result)
{
result = result.GetGenericArguments()[0];
}
else if (typeof(Task).IsAssignableFrom(taskType))
{
result = typeof(void);
}
else
{
result = null;
}

return typeof(Task).IsAssignableFrom(taskType) ? typeof(void) : null;
return result;

static Type? GetValueTaskType(Type valueTaskType)
{
Type? result;

if (valueTaskType == typeof(ValueTask))
{
result = typeof(void);
}
else if (valueTaskType.IsConstructedGenericType && valueTaskType.GetGenericTypeDefinition() == typeof(ValueTask<>))
{
result = valueTaskType.GetGenericArguments()[0];
}
else
{
result = null;
}

return result;
}
}

/// <summary>
Expand Down
15 changes: 10 additions & 5 deletions src/DotNext/Reflection/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static IEnumerable<Type> GetBaseTypes([DynamicallyAccessedMembers(Dynamic
{
for (var lookup = includeTopLevel ? type : type.BaseType; lookup is not null; lookup = lookup.BaseType)
yield return lookup;

if (includeInterfaces)
{
foreach (var iface in type.GetInterfaces())
Expand All @@ -88,11 +89,13 @@ public static IEnumerable<Type> GetBaseTypes([DynamicallyAccessedMembers(Dynamic

if (type.IsInterface)
goto exit;

if (abstractMethod.DeclaringType.IsInterface && abstractMethod.DeclaringType.IsAssignableFrom(type))
{
// Interface maps for generic interfaces on arrays cannot be retrieved.
if (type.IsArray && abstractMethod.DeclaringType.IsGenericType)
goto exit;

var interfaceMap = type.GetInterfaceMap(abstractMethod.DeclaringType);
for (var i = 0L; i < interfaceMap.InterfaceMethods.LongLength; i++)
{
Expand Down Expand Up @@ -123,11 +126,13 @@ public static IEnumerable<Type> GetBaseTypes([DynamicallyAccessedMembers(Dynamic
bool IsGenericInstanceOf(Type candidate)
=> candidate.IsConstructedGenericType && candidate.GetGenericTypeDefinition() == genericDefinition;

if (type.IsGenericTypeDefinition || !genericDefinition.IsGenericTypeDefinition)
return null;

if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == genericDefinition)
return type;
switch (type)
{
case { IsGenericTypeDefinition: true } when genericDefinition.IsGenericTypeDefinition is false:
return null;
case { IsConstructedGenericType: true } when type.GetGenericTypeDefinition() == genericDefinition:
return type;
}

switch (genericDefinition)
{
Expand Down

0 comments on commit 5a0ba33

Please sign in to comment.