Fixed serializing abstract base class ISerializable on .NET Core (#1712)
This commit is contained in:
@@ -1093,12 +1093,15 @@ namespace Newtonsoft.Json.Serialization
|
||||
JsonISerializableContract contract = new JsonISerializableContract(objectType);
|
||||
InitializeContract(contract);
|
||||
|
||||
ConstructorInfo constructorInfo = contract.NonNullableUnderlyingType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
|
||||
if (constructorInfo != null)
|
||||
if (contract.IsInstantiable)
|
||||
{
|
||||
ObjectConstructor<object> creator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(constructorInfo);
|
||||
ConstructorInfo constructorInfo = contract.NonNullableUnderlyingType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] {typeof(SerializationInfo), typeof(StreamingContext)}, null);
|
||||
if (constructorInfo != null)
|
||||
{
|
||||
ObjectConstructor<object> creator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(constructorInfo);
|
||||
|
||||
contract.ISerializableCreator = creator;
|
||||
contract.ISerializableCreator = creator;
|
||||
}
|
||||
}
|
||||
|
||||
return contract;
|
||||
|
||||
@@ -1706,6 +1706,11 @@ namespace Newtonsoft.Json.Serialization
|
||||
ThrowUnexpectedEndException(reader, contract, serializationInfo, "Unexpected end when deserializing object.");
|
||||
}
|
||||
|
||||
if (!contract.IsInstantiable)
|
||||
{
|
||||
throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
|
||||
}
|
||||
|
||||
if (contract.ISerializableCreator == null)
|
||||
{
|
||||
throw JsonSerializationException.Create(reader, "ISerializable type '{0}' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
|
||||
Reference in New Issue
Block a user