Skip to content

Commit

Permalink
Fix reflection issue with Unity/Mono (#23, #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Mar 5, 2022
1 parent 7c30d81 commit 21de2ab
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Tomlyn/Model/Accessors/ReflectionObjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,33 @@ public static ReflectionObjectInfo Get(Type type)
}

var interfaces = type.GetInterfaces();
Type? collectionType = null;
foreach (var i in interfaces)
{
if (i.IsGenericType)
{
// Match in priority IDictionary<,>
var genericTypeDefinition = i.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(IDictionary<,>))
{
var genericArguments = i.GetGenericArguments();
return new ReflectionObjectInfo(ReflectionObjectKind.Dictionary, genericArguments[0], genericArguments[1]);
}
if (genericTypeDefinition == typeof(ICollection<>))

if (collectionType is null && genericTypeDefinition == typeof(ICollection<>))
{
var genericArguments = i.GetGenericArguments();
return new ReflectionObjectInfo(ReflectionObjectKind.Collection, genericArguments[0]);
collectionType = i;
}
}
}

// Otherwise if we have a collection, match it
if (collectionType is not null)
{
var genericArguments = collectionType.GetGenericArguments();
return new ReflectionObjectInfo(ReflectionObjectKind.Collection, genericArguments[0]);
}

return new ReflectionObjectInfo(ReflectionObjectKind.Object);
}
}

0 comments on commit 21de2ab

Please sign in to comment.