DefaultValueHolder
Helper class used for calculating the default value for a given System.Type instance.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
namespace System.Text.Json.Serialization.Metadata
{
internal sealed class DefaultValueHolder
{
public object DefaultValue { get; }
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067:UnrecognizedReflectionPattern", Justification = "GetUninitializedObject is only called on a struct. You can always create an instance of a struct.")]
private DefaultValueHolder(Type type)
{
if (type.IsValueType && Nullable.GetUnderlyingType(type) == (Type)null)
DefaultValue = FormatterServices.GetUninitializedObject(type);
}
public bool IsDefaultValue(object value)
{
if (DefaultValue != null)
return DefaultValue.Equals(value);
return value == null;
}
public static DefaultValueHolder CreateHolder(Type type)
{
return new DefaultValueHolder(type);
}
}
}