From 4e8002bdcb6e0966fa9e620d141085b4e869ba6b Mon Sep 17 00:00:00 2001 From: JamesNK Date: Thu, 16 Oct 2008 06:16:39 +0000 Subject: [PATCH] -Misc refactoring --- Src/Newtonsoft.Json/JsonSerializer.cs | 4 ++-- .../Utilities/CollectionUtils.cs | 12 +++++----- .../Utilities/ReflectionUtils.cs | 22 +++++++++---------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index be80ea90..986d89f7 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -282,7 +282,7 @@ namespace Newtonsoft.Json { value = CreateJToken(reader); } - else if (typeof(IDictionary).IsAssignableFrom(objectType) || ReflectionUtils.ImplementsGenericInterfaceDefinition(objectType, typeof(IDictionary<,>))) + else if (typeof(IDictionary).IsAssignableFrom(objectType) || ReflectionUtils.ImplementsGenericDefinition(objectType, typeof(IDictionary<,>))) { if (existingValue == null) value = CreateAndPopulateDictionary(reader, objectType); @@ -493,7 +493,7 @@ namespace Newtonsoft.Json { currentValue = ReflectionUtils.GetMemberValue(memberMapping.Member, target); - useExistingValue = (currentValue != null && !memberType.IsArray && !ReflectionUtils.InheritsGenericClassDefinition(memberType, typeof(ReadOnlyCollection<>))); + useExistingValue = (currentValue != null && !memberType.IsArray && !ReflectionUtils.InheritsGenericDefinition(memberType, typeof(ReadOnlyCollection<>))); } diff --git a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs index fe656db3..9f7cbe13 100644 --- a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs @@ -360,7 +360,7 @@ namespace Newtonsoft.Json.Utilities return true; else if (typeof(IList).IsAssignableFrom(type)) return true; - else if (ReflectionUtils.ImplementsGenericInterfaceDefinition(type, typeof(IList<>))) + else if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IList<>))) return true; else return false; @@ -371,7 +371,7 @@ namespace Newtonsoft.Json.Utilities ValidationUtils.ArgumentNotNull(list, "list"); Type collectionDefinition; - if (ReflectionUtils.ImplementsGenericInterfaceDefinition(list.GetType(), typeof(ICollection<>), out collectionDefinition)) + if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(ICollection<>), out collectionDefinition)) { Type collectionItemType = ReflectionUtils.GetCollectionItemType(collectionDefinition); @@ -398,7 +398,7 @@ namespace Newtonsoft.Json.Utilities ValidationUtils.ArgumentNotNull(list, "list"); Type listDefinition; - if (ReflectionUtils.ImplementsGenericInterfaceDefinition(list.GetType(), typeof(IList<>), out listDefinition)) + if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(IList<>), out listDefinition)) { Type collectionItemType = ReflectionUtils.GetCollectionItemType(listDefinition); @@ -426,7 +426,7 @@ namespace Newtonsoft.Json.Utilities ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); Type dictionaryDefinition; - if (ReflectionUtils.ImplementsGenericInterfaceDefinition(dictionary.GetType(), typeof(IDictionary<,>), out dictionaryDefinition)) + if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary<,>), out dictionaryDefinition)) { Type dictionaryKeyType = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition); Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition); @@ -466,7 +466,7 @@ namespace Newtonsoft.Json.Utilities list = new List(); isReadOnlyOrFixedSize = true; } - else if (ReflectionUtils.InheritsGenericClassDefinition(listType, typeof(ReadOnlyCollection<>), out collectionType)) + else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection<>), out collectionType)) { Type readOnlyCollectionContentsType = collectionType.GetGenericArguments()[0]; Type genericEnumerable = ReflectionUtils.MakeGenericType(typeof(IEnumerable<>), readOnlyCollectionContentsType); @@ -522,7 +522,7 @@ namespace Newtonsoft.Json.Utilities { if (listType.IsArray) list = ToArray(((List)list).ToArray(), ReflectionUtils.GetCollectionItemType(listType)); - else if (ReflectionUtils.InheritsGenericClassDefinition(listType, typeof(ReadOnlyCollection<>))) + else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection<>))) list = (IList)ReflectionUtils.CreateInstance(listType, list); } diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index a46377aa..61786461 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -124,13 +124,13 @@ namespace Newtonsoft.Json.Utilities return !CollectionUtils.IsNullOrEmpty(property.GetIndexParameters()); } - public static bool ImplementsGenericInterfaceDefinition(Type type, Type genericInterfaceDefinition) + public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition) { Type implementingType; - return ImplementsGenericInterfaceDefinition(type, genericInterfaceDefinition, out implementingType); + return ImplementsGenericDefinition(type, genericInterfaceDefinition, out implementingType); } - public static bool ImplementsGenericInterfaceDefinition(Type type, Type genericInterfaceDefinition, out Type implementingType) + public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition, out Type implementingType) { ValidationUtils.ArgumentNotNull(type, "type"); ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, "genericInterfaceDefinition"); @@ -171,13 +171,13 @@ namespace Newtonsoft.Json.Utilities } - public static bool InheritsGenericClassDefinition(Type type, Type genericClassDefinition) + public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition) { Type implementingType; - return InheritsGenericClassDefinition(type, genericClassDefinition, out implementingType); + return InheritsGenericDefinition(type, genericClassDefinition, out implementingType); } - public static bool InheritsGenericClassDefinition(Type type, Type genericClassDefinition, out Type implementingType) + public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition, out Type implementingType) { ValidationUtils.ArgumentNotNull(type, "type"); ValidationUtils.ArgumentNotNull(genericClassDefinition, "genericClassDefinition"); @@ -185,10 +185,10 @@ namespace Newtonsoft.Json.Utilities if (!genericClassDefinition.IsClass || !genericClassDefinition.IsGenericTypeDefinition) throw new ArgumentNullException("'{0}' is not a generic class definition.".FormatWith(CultureInfo.InvariantCulture, genericClassDefinition)); - return InheritsGenericClassDefinitionInternal(type, type, genericClassDefinition, out implementingType); + return InheritsGenericDefinitionInternal(type, type, genericClassDefinition, out implementingType); } - private static bool InheritsGenericClassDefinitionInternal(Type initialType, Type currentType, Type genericClassDefinition, out Type implementingType) + private static bool InheritsGenericDefinitionInternal(Type initialType, Type currentType, Type genericClassDefinition, out Type implementingType) { if (currentType.IsGenericType) { @@ -207,7 +207,7 @@ namespace Newtonsoft.Json.Utilities return false; } - return InheritsGenericClassDefinitionInternal(initialType, currentType.BaseType, genericClassDefinition, out implementingType); + return InheritsGenericDefinitionInternal(initialType, currentType.BaseType, genericClassDefinition, out implementingType); } /// @@ -224,7 +224,7 @@ namespace Newtonsoft.Json.Utilities { return type.GetElementType(); } - else if (ImplementsGenericInterfaceDefinition(type, typeof(ICollection<>), out genericListType)) + else if (ImplementsGenericDefinition(type, typeof(ICollection<>), out genericListType)) { if (genericListType.IsGenericTypeDefinition) throw new Exception("Type {0} is not a collection.".FormatWith(CultureInfo.InvariantCulture, type)); @@ -246,7 +246,7 @@ namespace Newtonsoft.Json.Utilities ValidationUtils.ArgumentNotNull(dictionaryType, "type"); Type genericDictionaryType; - if (ImplementsGenericInterfaceDefinition(dictionaryType, typeof(IDictionary<,>), out genericDictionaryType)) + if (ImplementsGenericDefinition(dictionaryType, typeof(IDictionary<,>), out genericDictionaryType)) { if (genericDictionaryType.IsGenericTypeDefinition) throw new Exception("Type {0} is not a dictionary.".FormatWith(CultureInfo.InvariantCulture, dictionaryType));