-Refactored away dependency on ICustomAttributeProvider
This commit is contained in:
@@ -26,17 +26,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Newtonsoft.Json.Utilities;
|
using Newtonsoft.Json.Utilities;
|
||||||
#if NETFX_CORE || PORTABLE
|
|
||||||
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Newtonsoft.Json.Serialization
|
namespace Newtonsoft.Json.Serialization
|
||||||
{
|
{
|
||||||
internal static class CachedAttributeGetter<T> where T : Attribute
|
internal static class CachedAttributeGetter<T> where T : Attribute
|
||||||
{
|
{
|
||||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, T> TypeAttributeCache = new ThreadSafeStore<ICustomAttributeProvider, T>(JsonTypeReflector.GetAttribute<T>);
|
private static readonly ThreadSafeStore<object, T> TypeAttributeCache = new ThreadSafeStore<object, T>(JsonTypeReflector.GetAttribute<T>);
|
||||||
|
|
||||||
public static T GetAttribute(ICustomAttributeProvider type)
|
public static T GetAttribute(object type)
|
||||||
{
|
{
|
||||||
return TypeAttributeCache.Get(type);
|
return TypeAttributeCache.Get(type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,9 +44,6 @@ using Newtonsoft.Json.Converters;
|
|||||||
using Newtonsoft.Json.Utilities;
|
using Newtonsoft.Json.Utilities;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
#if NETFX_CORE || PORTABLE
|
|
||||||
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
|
|
||||||
#endif
|
|
||||||
#if NET20
|
#if NET20
|
||||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||||
#else
|
#else
|
||||||
@@ -295,10 +292,10 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
// add members that are explicitly marked with JsonProperty/DataMember attribute
|
// add members that are explicitly marked with JsonProperty/DataMember attribute
|
||||||
// or are a field if serializing just fields
|
// or are a field if serializing just fields
|
||||||
if (JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member.GetCustomAttributeProvider()) != null)
|
if (JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member) != null)
|
||||||
serializableMembers.Add(member);
|
serializableMembers.Add(member);
|
||||||
#if !NET20
|
#if !NET20
|
||||||
else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute<DataMemberAttribute>(member.GetCustomAttributeProvider()) != null)
|
else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute<DataMemberAttribute>(member) != null)
|
||||||
serializableMembers.Add(member);
|
serializableMembers.Add(member);
|
||||||
#endif
|
#endif
|
||||||
else if (memberSerialization == MemberSerialization.Fields && member.MemberType() == MemberTypes.Field)
|
else if (memberSerialization == MemberSerialization.Fields && member.MemberType() == MemberTypes.Field)
|
||||||
@@ -466,7 +463,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
property.PropertyType = parameterInfo.ParameterType;
|
property.PropertyType = parameterInfo.ParameterType;
|
||||||
|
|
||||||
bool allowNonPublicAccess;
|
bool allowNonPublicAccess;
|
||||||
SetPropertySettingsFromAttributes(property, parameterInfo.GetCustomAttributeProvider(), parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
|
SetPropertySettingsFromAttributes(property, parameterInfo, parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
|
||||||
|
|
||||||
property.Readable = false;
|
property.Readable = false;
|
||||||
property.Writable = true;
|
property.Writable = true;
|
||||||
@@ -500,7 +497,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
/// <returns>The contract's default <see cref="JsonConverter" />.</returns>
|
/// <returns>The contract's default <see cref="JsonConverter" />.</returns>
|
||||||
protected virtual JsonConverter ResolveContractConverter(Type objectType)
|
protected virtual JsonConverter ResolveContractConverter(Type objectType)
|
||||||
{
|
{
|
||||||
return JsonTypeReflector.GetJsonConverter(objectType.GetCustomAttributeProvider(), objectType);
|
return JsonTypeReflector.GetJsonConverter(objectType, objectType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Func<object> GetDefaultCreator(Type createdType)
|
private Func<object> GetDefaultCreator(Type createdType)
|
||||||
@@ -950,7 +947,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
property.ValueProvider = CreateMemberValueProvider(member);
|
property.ValueProvider = CreateMemberValueProvider(member);
|
||||||
|
|
||||||
bool allowNonPublicAccess;
|
bool allowNonPublicAccess;
|
||||||
SetPropertySettingsFromAttributes(property, member.GetCustomAttributeProvider(), member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
|
SetPropertySettingsFromAttributes(property, member, member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
|
||||||
|
|
||||||
if (memberSerialization != MemberSerialization.Fields)
|
if (memberSerialization != MemberSerialization.Fields)
|
||||||
{
|
{
|
||||||
@@ -970,17 +967,12 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
return property;
|
return property;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
|
private void SetPropertySettingsFromAttributes(JsonProperty property, object attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
|
||||||
{
|
{
|
||||||
#if !NET20
|
#if !NET20
|
||||||
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
|
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
|
||||||
|
|
||||||
MemberInfo memberInfo = null;
|
MemberInfo memberInfo = attributeProvider as MemberInfo;
|
||||||
#if !(NETFX_CORE || PORTABLE)
|
|
||||||
memberInfo = attributeProvider as MemberInfo;
|
|
||||||
#else
|
|
||||||
memberInfo = attributeProvider.UnderlyingObject as MemberInfo;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
DataMemberAttribute dataMemberAttribute;
|
DataMemberAttribute dataMemberAttribute;
|
||||||
if (dataContractAttribute != null && memberInfo != null)
|
if (dataContractAttribute != null && memberInfo != null)
|
||||||
|
|||||||
@@ -32,9 +32,6 @@ using System.Security;
|
|||||||
using System.Security.Permissions;
|
using System.Security.Permissions;
|
||||||
#endif
|
#endif
|
||||||
using Newtonsoft.Json.Utilities;
|
using Newtonsoft.Json.Utilities;
|
||||||
#if NETFX_CORE || PORTABLE
|
|
||||||
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
|
|
||||||
#endif
|
|
||||||
#if NET20
|
#if NET20
|
||||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||||
#else
|
#else
|
||||||
@@ -53,6 +50,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
internal static class JsonTypeReflector
|
internal static class JsonTypeReflector
|
||||||
{
|
{
|
||||||
|
private static bool? _dynamicCodeGeneration;
|
||||||
|
private static bool? _fullyTrusted;
|
||||||
|
|
||||||
public const string IdPropertyName = "$id";
|
public const string IdPropertyName = "$id";
|
||||||
public const string RefPropertyName = "$ref";
|
public const string RefPropertyName = "$ref";
|
||||||
public const string TypePropertyName = "$type";
|
public const string TypePropertyName = "$type";
|
||||||
@@ -62,7 +62,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
public const string ShouldSerializePrefix = "ShouldSerialize";
|
public const string ShouldSerializePrefix = "ShouldSerialize";
|
||||||
public const string SpecifiedPostfix = "Specified";
|
public const string SpecifiedPostfix = "Specified";
|
||||||
|
|
||||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> JsonConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetJsonConverterTypeFromAttribute);
|
private static readonly ThreadSafeStore<object, Type> JsonConverterTypeCache = new ThreadSafeStore<object, Type>(GetJsonConverterTypeFromAttribute);
|
||||||
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
|
||||||
private static readonly ThreadSafeStore<Type, Type> AssociatedMetadataTypesCache = new ThreadSafeStore<Type, Type>(GetAssociateMetadataTypeFromAttribute);
|
private static readonly ThreadSafeStore<Type, Type> AssociatedMetadataTypesCache = new ThreadSafeStore<Type, Type>(GetAssociateMetadataTypeFromAttribute);
|
||||||
|
|
||||||
@@ -71,9 +71,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
private static Type _cachedMetadataTypeAttributeType;
|
private static Type _cachedMetadataTypeAttributeType;
|
||||||
#endif
|
#endif
|
||||||
#if SILVERLIGHT
|
#if SILVERLIGHT
|
||||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> TypeConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetTypeConverterTypeFromAttribute);
|
private static readonly ThreadSafeStore<object, Type> TypeConverterTypeCache = new ThreadSafeStore<object, Type>(GetTypeConverterTypeFromAttribute);
|
||||||
|
|
||||||
private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
|
private static Type GetTypeConverterTypeFromAttribute(object attributeProvider)
|
||||||
{
|
{
|
||||||
TypeConverterAttribute converterAttribute = GetAttribute<TypeConverterAttribute>(attributeProvider);
|
TypeConverterAttribute converterAttribute = GetAttribute<TypeConverterAttribute>(attributeProvider);
|
||||||
if (converterAttribute == null)
|
if (converterAttribute == null)
|
||||||
@@ -82,7 +82,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
return Type.GetType(converterAttribute.ConverterTypeName);
|
return Type.GetType(converterAttribute.ConverterTypeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Type GetTypeConverterType(ICustomAttributeProvider attributeProvider)
|
private static Type GetTypeConverterType(object attributeProvider)
|
||||||
{
|
{
|
||||||
return TypeConverterTypeCache.Get(attributeProvider);
|
return TypeConverterTypeCache.Get(attributeProvider);
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
|
public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
|
||||||
{
|
{
|
||||||
return CachedAttributeGetter<JsonContainerAttribute>.GetAttribute(type.GetCustomAttributeProvider());
|
return CachedAttributeGetter<JsonContainerAttribute>.GetAttribute(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonObjectAttribute GetJsonObjectAttribute(Type type)
|
public static JsonObjectAttribute GetJsonObjectAttribute(Type type)
|
||||||
@@ -111,7 +111,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
public static SerializableAttribute GetSerializableAttribute(Type type)
|
public static SerializableAttribute GetSerializableAttribute(Type type)
|
||||||
{
|
{
|
||||||
return CachedAttributeGetter<SerializableAttribute>.GetAttribute(type.GetCustomAttributeProvider());
|
return CachedAttributeGetter<SerializableAttribute>.GetAttribute(type);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
public static DataContractAttribute GetDataContractAttribute(Type type)
|
public static DataContractAttribute GetDataContractAttribute(Type type)
|
||||||
{
|
{
|
||||||
// DataContractAttribute does not have inheritance
|
// DataContractAttribute does not have inheritance
|
||||||
return CachedAttributeGetter<DataContractAttribute>.GetAttribute(type.GetCustomAttributeProvider());
|
return CachedAttributeGetter<DataContractAttribute>.GetAttribute(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DataMemberAttribute GetDataMemberAttribute(MemberInfo memberInfo)
|
public static DataMemberAttribute GetDataMemberAttribute(MemberInfo memberInfo)
|
||||||
@@ -128,11 +128,11 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
// can't override a field
|
// can't override a field
|
||||||
if (memberInfo.MemberType() == MemberTypes.Field)
|
if (memberInfo.MemberType() == MemberTypes.Field)
|
||||||
return CachedAttributeGetter<DataMemberAttribute>.GetAttribute(memberInfo.GetCustomAttributeProvider());
|
return CachedAttributeGetter<DataMemberAttribute>.GetAttribute(memberInfo);
|
||||||
|
|
||||||
// search property and then search base properties if nothing is returned and the property is virtual
|
// search property and then search base properties if nothing is returned and the property is virtual
|
||||||
PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
|
PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
|
||||||
DataMemberAttribute result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(propertyInfo.GetCustomAttributeProvider());
|
DataMemberAttribute result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(propertyInfo);
|
||||||
if (result == null)
|
if (result == null)
|
||||||
{
|
{
|
||||||
if (propertyInfo.IsVirtual())
|
if (propertyInfo.IsVirtual())
|
||||||
@@ -143,7 +143,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
PropertyInfo baseProperty = (PropertyInfo)ReflectionUtils.GetMemberInfoFromType(currentType, propertyInfo);
|
PropertyInfo baseProperty = (PropertyInfo)ReflectionUtils.GetMemberInfoFromType(currentType, propertyInfo);
|
||||||
if (baseProperty != null && baseProperty.IsVirtual())
|
if (baseProperty != null && baseProperty.IsVirtual())
|
||||||
result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(baseProperty.GetCustomAttributeProvider());
|
result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(baseProperty);
|
||||||
|
|
||||||
currentType = currentType.BaseType();
|
currentType = currentType.BaseType();
|
||||||
}
|
}
|
||||||
@@ -179,12 +179,12 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
return MemberSerialization.OptOut;
|
return MemberSerialization.OptOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Type GetJsonConverterType(ICustomAttributeProvider attributeProvider)
|
private static Type GetJsonConverterType(object attributeProvider)
|
||||||
{
|
{
|
||||||
return JsonConverterTypeCache.Get(attributeProvider);
|
return JsonConverterTypeCache.Get(attributeProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
|
private static Type GetJsonConverterTypeFromAttribute(object attributeProvider)
|
||||||
{
|
{
|
||||||
JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
|
JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
|
||||||
return (converterAttribute != null)
|
return (converterAttribute != null)
|
||||||
@@ -192,15 +192,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
|
public static JsonConverter GetJsonConverter(object attributeProvider, Type targetConvertedType)
|
||||||
{
|
{
|
||||||
object provider = null;
|
|
||||||
#if !(NETFX_CORE || PORTABLE)
|
|
||||||
provider = attributeProvider as MemberInfo;
|
|
||||||
#else
|
|
||||||
provider = attributeProvider.UnderlyingObject;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Type converterType = GetJsonConverterType(attributeProvider);
|
Type converterType = GetJsonConverterType(attributeProvider);
|
||||||
|
|
||||||
if (converterType != null)
|
if (converterType != null)
|
||||||
@@ -284,13 +277,13 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
attribute = ReflectionUtils.GetAttribute<T>(type.GetCustomAttributeProvider(), true);
|
attribute = ReflectionUtils.GetAttribute<T>(type, true);
|
||||||
if (attribute != null)
|
if (attribute != null)
|
||||||
return attribute;
|
return attribute;
|
||||||
|
|
||||||
foreach (Type typeInterface in type.GetInterfaces())
|
foreach (Type typeInterface in type.GetInterfaces())
|
||||||
{
|
{
|
||||||
attribute = ReflectionUtils.GetAttribute<T>(typeInterface.GetCustomAttributeProvider(), true);
|
attribute = ReflectionUtils.GetAttribute<T>(typeInterface, true);
|
||||||
if (attribute != null)
|
if (attribute != null)
|
||||||
return attribute;
|
return attribute;
|
||||||
}
|
}
|
||||||
@@ -317,7 +310,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
attribute = ReflectionUtils.GetAttribute<T>(memberInfo.GetCustomAttributeProvider(), true);
|
attribute = ReflectionUtils.GetAttribute<T>(memberInfo, true);
|
||||||
if (attribute != null)
|
if (attribute != null)
|
||||||
return attribute;
|
return attribute;
|
||||||
|
|
||||||
@@ -329,7 +322,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
if (interfaceTypeMemberInfo != null)
|
if (interfaceTypeMemberInfo != null)
|
||||||
{
|
{
|
||||||
attribute = ReflectionUtils.GetAttribute<T>(interfaceTypeMemberInfo.GetCustomAttributeProvider(), true);
|
attribute = ReflectionUtils.GetAttribute<T>(interfaceTypeMemberInfo, true);
|
||||||
if (attribute != null)
|
if (attribute != null)
|
||||||
return attribute;
|
return attribute;
|
||||||
}
|
}
|
||||||
@@ -339,15 +332,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
|
public static T GetAttribute<T>(object provider) where T : Attribute
|
||||||
{
|
{
|
||||||
object provider = null;
|
|
||||||
#if !(NETFX_CORE || PORTABLE)
|
|
||||||
provider = attributeProvider;
|
|
||||||
#else
|
|
||||||
provider = attributeProvider.UnderlyingObject;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Type type = provider as Type;
|
Type type = provider as Type;
|
||||||
if (type != null)
|
if (type != null)
|
||||||
return GetAttribute<T>(type);
|
return GetAttribute<T>(type);
|
||||||
@@ -356,12 +342,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
if (memberInfo != null)
|
if (memberInfo != null)
|
||||||
return GetAttribute<T>(memberInfo);
|
return GetAttribute<T>(memberInfo);
|
||||||
|
|
||||||
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
|
return ReflectionUtils.GetAttribute<T>(provider, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool? _dynamicCodeGeneration;
|
|
||||||
private static bool? _fullyTrusted;
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
internal static void SetFullyTrusted(bool fullyTrusted)
|
internal static void SetFullyTrusted(bool fullyTrusted)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,9 +34,6 @@ using System.Globalization;
|
|||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
using System.Runtime.Serialization.Formatters;
|
using System.Runtime.Serialization.Formatters;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
#if NETFX_CORE || PORTABLE
|
|
||||||
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
|
|
||||||
#endif
|
|
||||||
#if NET20
|
#if NET20
|
||||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||||
#else
|
#else
|
||||||
@@ -56,21 +53,6 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
Other
|
Other
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class CustomAttributeProvider
|
|
||||||
{
|
|
||||||
private readonly object _underlyingObject;
|
|
||||||
|
|
||||||
public CustomAttributeProvider(object o)
|
|
||||||
{
|
|
||||||
_underlyingObject = o;
|
|
||||||
}
|
|
||||||
|
|
||||||
public object UnderlyingObject
|
|
||||||
{
|
|
||||||
get { return _underlyingObject; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Flags]
|
[Flags]
|
||||||
internal enum BindingFlags
|
internal enum BindingFlags
|
||||||
{
|
{
|
||||||
@@ -110,15 +92,6 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ICustomAttributeProvider GetCustomAttributeProvider(this object o)
|
|
||||||
{
|
|
||||||
#if !(NETFX_CORE || PORTABLE)
|
|
||||||
return (ICustomAttributeProvider)o;
|
|
||||||
#else
|
|
||||||
return new ICustomAttributeProvider(o);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool IsVirtual(this PropertyInfo propertyInfo)
|
public static bool IsVirtual(this PropertyInfo propertyInfo)
|
||||||
{
|
{
|
||||||
ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo");
|
ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo");
|
||||||
@@ -582,7 +555,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
case MemberTypes.Property:
|
case MemberTypes.Property:
|
||||||
PropertyInfo propertyInfo = (PropertyInfo) member;
|
PropertyInfo propertyInfo = (PropertyInfo)member;
|
||||||
|
|
||||||
if (!propertyInfo.CanRead)
|
if (!propertyInfo.CanRead)
|
||||||
return false;
|
return false;
|
||||||
@@ -686,12 +659,12 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
|
public static T GetAttribute<T>(object attributeProvider) where T : Attribute
|
||||||
{
|
{
|
||||||
return GetAttribute<T>(attributeProvider, true);
|
return GetAttribute<T>(attributeProvider, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
|
public static T GetAttribute<T>(object attributeProvider, bool inherit) where T : Attribute
|
||||||
{
|
{
|
||||||
T[] attributes = GetAttributes<T>(attributeProvider, inherit);
|
T[] attributes = GetAttributes<T>(attributeProvider, inherit);
|
||||||
|
|
||||||
@@ -699,7 +672,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if !(NETFX_CORE || PORTABLE)
|
#if !(NETFX_CORE || PORTABLE)
|
||||||
public static T[] GetAttributes<T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
|
public static T[] GetAttributes<T>(object attributeProvider, bool inherit) where T : Attribute
|
||||||
{
|
{
|
||||||
ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider");
|
ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider");
|
||||||
|
|
||||||
@@ -723,13 +696,11 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
if (provider is ParameterInfo)
|
if (provider is ParameterInfo)
|
||||||
return (T[])Attribute.GetCustomAttributes((ParameterInfo)provider, typeof(T), inherit);
|
return (T[])Attribute.GetCustomAttributes((ParameterInfo)provider, typeof(T), inherit);
|
||||||
|
|
||||||
return (T[])attributeProvider.GetCustomAttributes(typeof(T), inherit);
|
return (T[])((ICustomAttributeProvider)attributeProvider).GetCustomAttributes(typeof(T), inherit);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
public static T[] GetAttributes<T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
|
public static T[] GetAttributes<T>(object provider, bool inherit) where T : Attribute
|
||||||
{
|
{
|
||||||
object provider = attributeProvider.UnderlyingObject;
|
|
||||||
|
|
||||||
if (provider is Type)
|
if (provider is Type)
|
||||||
return ((Type)provider).GetTypeInfo().GetCustomAttributes<T>(inherit).ToArray();
|
return ((Type)provider).GetTypeInfo().GetCustomAttributes<T>(inherit).ToArray();
|
||||||
|
|
||||||
@@ -760,7 +731,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
public static object CreateGeneric(Type genericTypeDefinition, Type innerType, params object[] args)
|
public static object CreateGeneric(Type genericTypeDefinition, Type innerType, params object[] args)
|
||||||
{
|
{
|
||||||
return CreateGeneric(genericTypeDefinition, new [] { innerType }, args);
|
return CreateGeneric(genericTypeDefinition, new[] { innerType }, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static object CreateGeneric(Type genericTypeDefinition, IList<Type> innerTypes, params object[] args)
|
public static object CreateGeneric(Type genericTypeDefinition, IList<Type> innerTypes, params object[] args)
|
||||||
@@ -779,12 +750,12 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return instanceCreator(specificType, args);
|
return instanceCreator(specificType, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static object CreateInstance(Type type, params object[] args)
|
public static object CreateInstance(Type type, params object[] args)
|
||||||
{
|
{
|
||||||
ValidationUtils.ArgumentNotNull(type, "type");
|
ValidationUtils.ArgumentNotNull(type, "type");
|
||||||
|
|
||||||
return Activator.CreateInstance(type, args);
|
return Activator.CreateInstance(type, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SplitFullyQualifiedTypeName(string fullyQualifiedTypeName, out string typeName, out string assemblyName)
|
public static void SplitFullyQualifiedTypeName(string fullyQualifiedTypeName, out string typeName, out string assemblyName)
|
||||||
{
|
{
|
||||||
@@ -836,7 +807,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
switch (memberInfo.MemberType())
|
switch (memberInfo.MemberType())
|
||||||
{
|
{
|
||||||
case MemberTypes.Property:
|
case MemberTypes.Property:
|
||||||
PropertyInfo propertyInfo = (PropertyInfo) memberInfo;
|
PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
|
||||||
|
|
||||||
Type[] types = propertyInfo.GetIndexParameters().Select(p => p.ParameterType).ToArray();
|
Type[] types = propertyInfo.GetIndexParameters().Select(p => p.ParameterType).ToArray();
|
||||||
|
|
||||||
@@ -947,7 +918,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
bool isMethodOverriden = currentType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
bool isMethodOverriden = currentType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
||||||
.Any(info =>
|
.Any(info =>
|
||||||
info.Name == method &&
|
info.Name == method &&
|
||||||
// check that the method overrides the original on DynamicObjectProxy
|
// check that the method overrides the original on DynamicObjectProxy
|
||||||
info.DeclaringType != methodDeclaringType
|
info.DeclaringType != methodDeclaringType
|
||||||
&& info.GetBaseDefinition().DeclaringType == methodDeclaringType
|
&& info.GetBaseDefinition().DeclaringType == methodDeclaringType
|
||||||
);
|
);
|
||||||
@@ -993,6 +964,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
case PrimitiveTypeCode.DateTimeOffset:
|
case PrimitiveTypeCode.DateTimeOffset:
|
||||||
return new DateTimeOffset();
|
return new DateTimeOffset();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsNullable(type))
|
if (IsNullable(type))
|
||||||
|
|||||||
Reference in New Issue
Block a user