Skip to content

Commit 3a08dde

Browse files
authored
Fix Type.GetGenericTypeDefinition() interface error (#166)
1 parent 3ea2f72 commit 3a08dde

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Hyperion.Tests/GenericDictionarySerializerTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,15 @@ public ProtectedCustomDictionary(Dictionary<TKey, TValue> dict) : base(new Dicti
7171
{ }
7272
}
7373

74+
// Dictionary serializer fails to fetch the generic IDictionary interface if
75+
// Type.GetInterfaces() returns a non-generic interface before the IDictionary interface
7476
/// <summary>
7577
/// Just a custom class wrapper for another <see cref="IDictionary{TKey,TValue}"/>
7678
/// </summary>
77-
class CustomDictionary<TKey, TValue> : IDictionary<TKey, TValue>
79+
class CustomDictionary<TKey, TValue> :
80+
IEnumerable,
81+
IDictionary<TKey, TValue>,
82+
IEquatable<CustomDictionary<TKey, TValue>>
7883
{
7984
private readonly IDictionary<TKey, TValue> _dictGeneric;
8085

@@ -162,6 +167,11 @@ public bool TryGetValue(TKey key, out TValue value)
162167
return _dictGeneric.TryGetValue(key, out value);
163168
}
164169

170+
public bool Equals(CustomDictionary<TKey, TValue> other)
171+
{
172+
return true;
173+
}
174+
165175
/// <inheritdoc />
166176
public TValue this[TKey key]
167177
{

src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type
9797

9898
private GenericDictionaryTypes GetKeyValuePairType(Type dictImplementationType)
9999
{
100-
var dictInterface = dictImplementationType.GetInterfaces().First(i => i.GetGenericTypeDefinition() == typeof (IDictionary<,>));
100+
var dictInterface = dictImplementationType
101+
.GetInterfaces()
102+
.Where(i => i.GetTypeInfo().IsGenericType)
103+
.First(i => i.GetGenericTypeDefinition() == typeof(IDictionary<,>));
101104
var keyType = dictInterface.GetGenericArguments()[0];
102105
var valueType = dictInterface.GetGenericArguments()[1];
103106
return new GenericDictionaryTypes()

0 commit comments

Comments
 (0)