diff --git a/src/Discord.Net/Net/Converters/DiscordContractResolver.cs b/src/Discord.Net/Net/Converters/DiscordContractResolver.cs index e240c2017..256afe866 100644 --- a/src/Discord.Net/Net/Converters/DiscordContractResolver.cs +++ b/src/Discord.Net/Net/Converters/DiscordContractResolver.cs @@ -55,17 +55,16 @@ namespace Discord.Net.Converters converter = ImageConverter.Instance; else if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Optional<>)) { - var innerType = type.GenericTypeArguments[0]; var typeInput = propInfo.DeclaringType; - var typeOutput = propInfo.PropertyType; + var innerTypeOutput = type.GenericTypeArguments[0]; - var getter = typeof(Func<,>).MakeGenericType(typeInput, typeOutput); + var getter = typeof(Func<,>).MakeGenericType(typeInput, type); var getterDelegate = propInfo.GetMethod.CreateDelegate(getter); - var shouldSerialize = _shouldSerialize.MakeGenericMethod(typeInput, typeOutput); + var shouldSerialize = _shouldSerialize.MakeGenericMethod(typeInput, innerTypeOutput); var shouldSerializeDelegate = (Func)shouldSerialize.CreateDelegate(typeof(Func)); property.ShouldSerialize = x => shouldSerializeDelegate(x, getterDelegate); - var converterType = typeof(OptionalConverter<>).MakeGenericType(innerType); + var converterType = typeof(OptionalConverter<>).MakeGenericType(innerTypeOutput); converter = converterType.GetTypeInfo().GetDeclaredField("Instance").GetValue(null) as JsonConverter; } } @@ -81,9 +80,8 @@ namespace Discord.Net.Converters } private static bool ShouldSerialize(object owner, Delegate getter) - where TValue : IOptional { - return (getter as Func)((TOwner)owner).IsSpecified; + return (getter as Func>)((TOwner)owner).IsSpecified; } } }