Browse Source

Removed IOptional from contract resolver

tags/1.0-rc
RogueException 9 years ago
parent
commit
fdb6c914e6
1 changed files with 5 additions and 7 deletions
  1. +5
    -7
      src/Discord.Net/Net/Converters/DiscordContractResolver.cs

+ 5
- 7
src/Discord.Net/Net/Converters/DiscordContractResolver.cs View File

@@ -55,17 +55,16 @@ namespace Discord.Net.Converters
converter = ImageConverter.Instance; converter = ImageConverter.Instance;
else if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Optional<>)) else if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Optional<>))
{ {
var innerType = type.GenericTypeArguments[0];
var typeInput = propInfo.DeclaringType; 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 getterDelegate = propInfo.GetMethod.CreateDelegate(getter);
var shouldSerialize = _shouldSerialize.MakeGenericMethod(typeInput, typeOutput);
var shouldSerialize = _shouldSerialize.MakeGenericMethod(typeInput, innerTypeOutput);
var shouldSerializeDelegate = (Func<object, Delegate, bool>)shouldSerialize.CreateDelegate(typeof(Func<object, Delegate, bool>)); var shouldSerializeDelegate = (Func<object, Delegate, bool>)shouldSerialize.CreateDelegate(typeof(Func<object, Delegate, bool>));
property.ShouldSerialize = x => shouldSerializeDelegate(x, getterDelegate); 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; converter = converterType.GetTypeInfo().GetDeclaredField("Instance").GetValue(null) as JsonConverter;
} }
} }
@@ -81,9 +80,8 @@ namespace Discord.Net.Converters
} }


private static bool ShouldSerialize<TOwner, TValue>(object owner, Delegate getter) private static bool ShouldSerialize<TOwner, TValue>(object owner, Delegate getter)
where TValue : IOptional
{ {
return (getter as Func<TOwner, TValue>)((TOwner)owner).IsSpecified;
return (getter as Func<TOwner, Optional<TValue>>)((TOwner)owner).IsSpecified;
} }
} }
} }

Loading…
Cancel
Save