@@ -6,7 +6,6 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Discord.Net.Converters
|
||||
{
|
||||
@@ -29,11 +28,11 @@ namespace Discord.Net.Converters
|
||||
var typeInput = propInfo.DeclaringType;
|
||||
var innerTypeOutput = type.GenericTypeArguments[0];
|
||||
|
||||
var parentArg = Expression.Parameter(typeof(object));
|
||||
var optional = Expression.Property(Expression.Convert(parentArg, property.DeclaringType), member as PropertyInfo);
|
||||
var isSpecified = Expression.Property(optional, "IsSpecified");
|
||||
var lambda = Expression.Lambda<Func<object, bool>>(isSpecified, parentArg).Compile();
|
||||
property.ShouldSerialize = x => lambda(x);
|
||||
var getter = typeof(Func<,>).MakeGenericType(typeInput, type);
|
||||
var getterDelegate = propInfo.GetMethod.CreateDelegate(getter);
|
||||
var shouldSerialize = _shouldSerialize.MakeGenericMethod(typeInput, innerTypeOutput);
|
||||
var shouldSerializeDelegate = (Func<object, Delegate, bool>)shouldSerialize.CreateDelegate(typeof(Func<object, Delegate, bool>));
|
||||
property.ShouldSerialize = x => shouldSerializeDelegate(x, getterDelegate);
|
||||
|
||||
var converterType = typeof(OptionalConverter<>).MakeGenericType(innerTypeOutput).GetTypeInfo();
|
||||
var instanceField = converterType.GetDeclaredField("Instance");
|
||||
@@ -97,5 +96,10 @@ namespace Discord.Net.Converters
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool ShouldSerialize<TOwner, TValue>(object owner, Delegate getter)
|
||||
{
|
||||
return (getter as Func<TOwner, Optional<TValue>>)((TOwner)owner).IsSpecified;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
"System.Collections.Immutable": "1.2.0-rc2-24027",
|
||||
"System.IO.Compression": "4.1.0-rc2-24027",
|
||||
"System.IO.FileSystem": "4.0.1-rc2-24027",
|
||||
"System.Linq.Expressions": "4.0.11-rc2-24027",
|
||||
"System.Net.Http": "4.0.1-rc2-24027",
|
||||
"System.Net.WebSockets.Client": "4.0.0-rc2-24027",
|
||||
"System.Reflection.Extensions": "4.0.1-rc2-24027",
|
||||
|
||||
Reference in New Issue
Block a user