-Misc refactoring

This commit is contained in:
JamesNK
2008-10-16 06:16:39 +00:00
parent 68af212cfb
commit 4e8002bdcb
3 changed files with 19 additions and 19 deletions
+2 -2
View File
@@ -282,7 +282,7 @@ namespace Newtonsoft.Json
{ {
value = CreateJToken(reader); 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) if (existingValue == null)
value = CreateAndPopulateDictionary(reader, objectType); value = CreateAndPopulateDictionary(reader, objectType);
@@ -493,7 +493,7 @@ namespace Newtonsoft.Json
{ {
currentValue = ReflectionUtils.GetMemberValue(memberMapping.Member, target); 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<>)));
} }
@@ -360,7 +360,7 @@ namespace Newtonsoft.Json.Utilities
return true; return true;
else if (typeof(IList).IsAssignableFrom(type)) else if (typeof(IList).IsAssignableFrom(type))
return true; return true;
else if (ReflectionUtils.ImplementsGenericInterfaceDefinition(type, typeof(IList<>))) else if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IList<>)))
return true; return true;
else else
return false; return false;
@@ -371,7 +371,7 @@ namespace Newtonsoft.Json.Utilities
ValidationUtils.ArgumentNotNull(list, "list"); ValidationUtils.ArgumentNotNull(list, "list");
Type collectionDefinition; 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); Type collectionItemType = ReflectionUtils.GetCollectionItemType(collectionDefinition);
@@ -398,7 +398,7 @@ namespace Newtonsoft.Json.Utilities
ValidationUtils.ArgumentNotNull(list, "list"); ValidationUtils.ArgumentNotNull(list, "list");
Type listDefinition; 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); Type collectionItemType = ReflectionUtils.GetCollectionItemType(listDefinition);
@@ -426,7 +426,7 @@ namespace Newtonsoft.Json.Utilities
ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
Type dictionaryDefinition; 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 dictionaryKeyType = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition);
Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition); Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition);
@@ -466,7 +466,7 @@ namespace Newtonsoft.Json.Utilities
list = new List<object>(); list = new List<object>();
isReadOnlyOrFixedSize = true; 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 readOnlyCollectionContentsType = collectionType.GetGenericArguments()[0];
Type genericEnumerable = ReflectionUtils.MakeGenericType(typeof(IEnumerable<>), readOnlyCollectionContentsType); Type genericEnumerable = ReflectionUtils.MakeGenericType(typeof(IEnumerable<>), readOnlyCollectionContentsType);
@@ -522,7 +522,7 @@ namespace Newtonsoft.Json.Utilities
{ {
if (listType.IsArray) if (listType.IsArray)
list = ToArray(((List<object>)list).ToArray(), ReflectionUtils.GetCollectionItemType(listType)); list = ToArray(((List<object>)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); list = (IList)ReflectionUtils.CreateInstance(listType, list);
} }
@@ -124,13 +124,13 @@ namespace Newtonsoft.Json.Utilities
return !CollectionUtils.IsNullOrEmpty<ParameterInfo>(property.GetIndexParameters()); return !CollectionUtils.IsNullOrEmpty<ParameterInfo>(property.GetIndexParameters());
} }
public static bool ImplementsGenericInterfaceDefinition(Type type, Type genericInterfaceDefinition) public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition)
{ {
Type implementingType; 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(type, "type");
ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, "genericInterfaceDefinition"); 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; 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(type, "type");
ValidationUtils.ArgumentNotNull(genericClassDefinition, "genericClassDefinition"); ValidationUtils.ArgumentNotNull(genericClassDefinition, "genericClassDefinition");
@@ -185,10 +185,10 @@ namespace Newtonsoft.Json.Utilities
if (!genericClassDefinition.IsClass || !genericClassDefinition.IsGenericTypeDefinition) if (!genericClassDefinition.IsClass || !genericClassDefinition.IsGenericTypeDefinition)
throw new ArgumentNullException("'{0}' is not a generic class definition.".FormatWith(CultureInfo.InvariantCulture, genericClassDefinition)); 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) if (currentType.IsGenericType)
{ {
@@ -207,7 +207,7 @@ namespace Newtonsoft.Json.Utilities
return false; return false;
} }
return InheritsGenericClassDefinitionInternal(initialType, currentType.BaseType, genericClassDefinition, out implementingType); return InheritsGenericDefinitionInternal(initialType, currentType.BaseType, genericClassDefinition, out implementingType);
} }
/// <summary> /// <summary>
@@ -224,7 +224,7 @@ namespace Newtonsoft.Json.Utilities
{ {
return type.GetElementType(); return type.GetElementType();
} }
else if (ImplementsGenericInterfaceDefinition(type, typeof(ICollection<>), out genericListType)) else if (ImplementsGenericDefinition(type, typeof(ICollection<>), out genericListType))
{ {
if (genericListType.IsGenericTypeDefinition) if (genericListType.IsGenericTypeDefinition)
throw new Exception("Type {0} is not a collection.".FormatWith(CultureInfo.InvariantCulture, type)); 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"); ValidationUtils.ArgumentNotNull(dictionaryType, "type");
Type genericDictionaryType; Type genericDictionaryType;
if (ImplementsGenericInterfaceDefinition(dictionaryType, typeof(IDictionary<,>), out genericDictionaryType)) if (ImplementsGenericDefinition(dictionaryType, typeof(IDictionary<,>), out genericDictionaryType))
{ {
if (genericDictionaryType.IsGenericTypeDefinition) if (genericDictionaryType.IsGenericTypeDefinition)
throw new Exception("Type {0} is not a dictionary.".FormatWith(CultureInfo.InvariantCulture, dictionaryType)); throw new Exception("Type {0} is not a dictionary.".FormatWith(CultureInfo.InvariantCulture, dictionaryType));