From c7fd4d5e298b9462774a52fd48c66800e6380095 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 8 Dec 2013 13:37:13 +1300 Subject: [PATCH] -WinRT build --- .../Converters/JsonValueConverterTests.cs | 2 +- .../Linq/JsonPath/QueryExpressionTests.cs | 6 + .../Newtonsoft.Json.Tests.WinRT.csproj | 5 +- .../Serialization/JsonSerializerTest.cs | 22 - .../Converters/DiscriminatedUnionConverter.cs | 11 +- .../Newtonsoft.Json.WinRT.csproj | 11 +- .../Utilities/ReflectionUtils.cs | 78 +-- .../Utilities/TypeExtensions.cs | 638 +++++++++--------- 8 files changed, 390 insertions(+), 383 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Converters/JsonValueConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/JsonValueConverterTests.cs index d8ed6a17..d9e00e06 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/JsonValueConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/JsonValueConverterTests.cs @@ -33,13 +33,13 @@ using NUnit.Framework; using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; +#endif using Windows.Data.Json; using System.IO; using System.Diagnostics; using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.Linq; -#endif namespace Newtonsoft.Json.Tests.Converters { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JsonPath/QueryExpressionTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JsonPath/QueryExpressionTests.cs index f918bd13..c7d9b21d 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JsonPath/QueryExpressionTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JsonPath/QueryExpressionTests.cs @@ -5,7 +5,13 @@ using System.Text; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq.JsonPath; +#if !NETFX_CORE using NUnit.Framework; +#else +using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; +using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; +using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; +#endif namespace Newtonsoft.Json.Tests.Linq.JsonPath { diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.WinRT.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.WinRT.csproj index a4ae6f7b..b7088aea 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.WinRT.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.WinRT.csproj @@ -142,9 +142,11 @@ - + + + @@ -177,7 +179,6 @@ - diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 77d3ab7d..07a9d0b8 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -9471,28 +9471,6 @@ Parameter name: value", ]", json); } - public class Foo2 - { - public string Name { get; set; } - } - - [Test] - [Ignore] - public void SerializeMultipleObjectsFromStream() - { - StringReader sr = new StringReader(@"{ ""name"": ""bar"" }{ ""name"": ""baz"" }"); - - var reader1 = new JsonTextReader(sr); - var serializer1 = new JsonSerializer { }; - var foo1 = serializer1.Deserialize(reader1); - Assert.AreEqual("bar", foo1.Name); - - var reader2 = new JsonTextReader(sr); - var serializer2 = new JsonSerializer { }; - var foo2 = serializer2.Deserialize(reader2); - Assert.AreEqual("baz", foo2.Name); - } - #if !(PORTABLE || PORTABLE40 || NETFX_CORE) [Test] public void SerializeDictionaryWithStructKey() diff --git a/Src/Newtonsoft.Json/Converters/DiscriminatedUnionConverter.cs b/Src/Newtonsoft.Json/Converters/DiscriminatedUnionConverter.cs index 2f31b8de..fba3603e 100644 --- a/Src/Newtonsoft.Json/Converters/DiscriminatedUnionConverter.cs +++ b/Src/Newtonsoft.Json/Converters/DiscriminatedUnionConverter.cs @@ -27,7 +27,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; -using System.Data.Odbc; +using System.Linq; using System.Reflection; using Newtonsoft.Json.Serialization; using System.Globalization; @@ -145,7 +145,12 @@ namespace Newtonsoft.Json.Converters // all fsharp objects have CompilationMappingAttribute // get the fsharp assembly from the attribute and initialize latebound methods - object[] attributes = objectType.GetCustomAttributes(true); + object[] attributes; +#if !(NETFX_CORE || PORTABLE) + attributes = objectType.GetCustomAttributes(true); +#else + attributes = objectType.GetTypeInfo().GetCustomAttributes(true).ToArray(); +#endif bool isFSharpType = false; foreach (object attribute in attributes) { @@ -171,7 +176,7 @@ namespace Newtonsoft.Json.Converters { _initialized = true; - Assembly fsharpCoreAssembly = attributeType.Assembly; + Assembly fsharpCoreAssembly = attributeType.Assembly(); Type fsharpType = fsharpCoreAssembly.GetType("Microsoft.FSharp.Reflection.FSharpType"); MethodInfo isUnionMethodInfo = fsharpType.GetMethod("IsUnion", BindingFlags.Public | BindingFlags.Static); diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.WinRT.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.WinRT.csproj index 0aa9fce5..85c78d1b 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.WinRT.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.WinRT.csproj @@ -127,6 +127,7 @@ + @@ -178,11 +179,19 @@ - + + + + + + + + + diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 646fa393..c99c48f4 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -55,30 +55,30 @@ namespace Newtonsoft.Json.Utilities #endif #if NETFX_CORE || PORTABLE - [Flags] - internal enum BindingFlags - { - Default = 0, - IgnoreCase = 1, - DeclaredOnly = 2, - Instance = 4, - Static = 8, - Public = 16, - NonPublic = 32, - FlattenHierarchy = 64, - InvokeMethod = 256, - CreateInstance = 512, - GetField = 1024, - SetField = 2048, - GetProperty = 4096, - SetProperty = 8192, - PutDispProperty = 16384, - ExactBinding = 65536, - PutRefDispProperty = 32768, - SuppressChangeType = 131072, - OptionalParamBinding = 262144, - IgnoreReturn = 16777216 - } + [Flags] + internal enum BindingFlags + { + Default = 0, + IgnoreCase = 1, + DeclaredOnly = 2, + Instance = 4, + Static = 8, + Public = 16, + NonPublic = 32, + FlattenHierarchy = 64, + InvokeMethod = 256, + CreateInstance = 512, + GetField = 1024, + SetField = 2048, + GetProperty = 4096, + SetProperty = 8192, + PutDispProperty = 16384, + ExactBinding = 65536, + PutRefDispProperty = 32768, + SuppressChangeType = 131072, + OptionalParamBinding = 262144, + IgnoreReturn = 16777216 + } #endif internal static class ReflectionUtils @@ -154,7 +154,7 @@ namespace Newtonsoft.Json.Utilities fullyQualifiedTypeName = t.AssemblyQualifiedName; } #else - fullyQualifiedTypeName = t.AssemblyQualifiedName; + fullyQualifiedTypeName = t.AssemblyQualifiedName; #endif switch (assemblyFormat) @@ -690,25 +690,25 @@ namespace Newtonsoft.Json.Utilities throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); } #else - public static T[] GetAttributes(object provider, bool inherit) where T : Attribute - { - if (provider is Type) - return ((Type)provider).GetTypeInfo().GetCustomAttributes(inherit).ToArray(); + public static T[] GetAttributes(object provider, bool inherit) where T : Attribute + { + if (provider is Type) + return ((Type) provider).GetTypeInfo().GetCustomAttributes(inherit).ToArray(); - if (provider is Assembly) - return ((Assembly)provider).GetCustomAttributes().ToArray(); + if (provider is Assembly) + return ((Assembly) provider).GetCustomAttributes().ToArray(); - if (provider is MemberInfo) - return ((MemberInfo)provider).GetCustomAttributes(inherit).ToArray(); + if (provider is MemberInfo) + return ((MemberInfo) provider).GetCustomAttributes(inherit).ToArray(); - if (provider is Module) - return ((Module)provider).GetCustomAttributes().ToArray(); + if (provider is Module) + return ((Module) provider).GetCustomAttributes().ToArray(); - if (provider is ParameterInfo) - return ((ParameterInfo)provider).GetCustomAttributes(inherit).ToArray(); + if (provider is ParameterInfo) + return ((ParameterInfo) provider).GetCustomAttributes(inherit).ToArray(); - throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); - } + throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); + } #endif public static void SplitFullyQualifiedTypeName(string fullyQualifiedTypeName, out string typeName, out string assemblyName) diff --git a/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs b/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs index 333f6f01..4cd71194 100644 --- a/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs +++ b/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs @@ -30,7 +30,6 @@ using System.Reflection; using Newtonsoft.Json.Utilities.LinqBridge; #else using System.Linq; - #endif namespace Newtonsoft.Json.Utilities @@ -38,45 +37,45 @@ namespace Newtonsoft.Json.Utilities internal static class TypeExtensions { #if NETFX_CORE || PORTABLE - private static BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance; + private static BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance; - public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo) - { - return propertyInfo.GetGetMethod(false); - } + public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo) + { + return propertyInfo.GetGetMethod(false); + } - public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo, bool nonPublic) - { - MethodInfo getMethod = propertyInfo.GetMethod; - if (getMethod != null && (getMethod.IsPublic || nonPublic)) - return getMethod; + public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo, bool nonPublic) + { + MethodInfo getMethod = propertyInfo.GetMethod; + if (getMethod != null && (getMethod.IsPublic || nonPublic)) + return getMethod; - return null; - } + return null; + } - public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo) - { - return propertyInfo.GetSetMethod(false); - } + public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo) + { + return propertyInfo.GetSetMethod(false); + } - public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo, bool nonPublic) - { - MethodInfo setMethod = propertyInfo.SetMethod; - if (setMethod != null && (setMethod.IsPublic || nonPublic)) - return setMethod; + public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo, bool nonPublic) + { + MethodInfo setMethod = propertyInfo.SetMethod; + if (setMethod != null && (setMethod.IsPublic || nonPublic)) + return setMethod; - return null; - } + return null; + } - public static bool IsSubclassOf(this Type type, Type c) - { - return type.GetTypeInfo().IsSubclassOf(c); - } + public static bool IsSubclassOf(this Type type, Type c) + { + return type.GetTypeInfo().IsSubclassOf(c); + } - public static bool IsAssignableFrom(this Type type, Type c) - { - return type.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo()); - } + public static bool IsAssignableFrom(this Type type, Type c) + { + return type.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo()); + } #endif public static MethodInfo Method(this Delegate d) @@ -84,7 +83,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return d.Method; #else - return d.GetMethodInfo(); + return d.GetMethodInfo(); #endif } @@ -111,7 +110,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.ContainsGenericParameters; #else - return type.GetTypeInfo().ContainsGenericParameters; + return type.GetTypeInfo().ContainsGenericParameters; #endif } @@ -120,7 +119,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsInterface; #else - return type.GetTypeInfo().IsInterface; + return type.GetTypeInfo().IsInterface; #endif } @@ -129,7 +128,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsGenericType; #else - return type.GetTypeInfo().IsGenericType; + return type.GetTypeInfo().IsGenericType; #endif } @@ -138,7 +137,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsGenericTypeDefinition; #else - return type.GetTypeInfo().IsGenericTypeDefinition; + return type.GetTypeInfo().IsGenericTypeDefinition; #endif } @@ -147,7 +146,16 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.BaseType; #else - return type.GetTypeInfo().BaseType; + return type.GetTypeInfo().BaseType; +#endif + } + + public static Assembly Assembly(this Type type) + { +#if !(NETFX_CORE || PORTABLE) + return type.Assembly; +#else + return type.GetTypeInfo().Assembly; #endif } @@ -156,7 +164,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsEnum; #else - return type.GetTypeInfo().IsEnum; + return type.GetTypeInfo().IsEnum; #endif } @@ -165,7 +173,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsClass; #else - return type.GetTypeInfo().IsClass; + return type.GetTypeInfo().IsClass; #endif } @@ -174,7 +182,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsSealed; #else - return type.GetTypeInfo().IsSealed; + return type.GetTypeInfo().IsSealed; #endif } @@ -214,293 +222,293 @@ namespace Newtonsoft.Json.Utilities #endif #if (NETFX_CORE || PORTABLE) - public static MethodInfo GetBaseDefinition(this MethodInfo method) - { - return method.GetRuntimeBaseDefinition(); - } + public static MethodInfo GetBaseDefinition(this MethodInfo method) + { + return method.GetRuntimeBaseDefinition(); + } #endif #if (NETFX_CORE || PORTABLE) - public static bool IsDefined(this Type type, Type attributeType, bool inherit) - { - return type.GetTypeInfo().CustomAttributes.Any(a => a.AttributeType == attributeType); - } - - public static MethodInfo GetMethod(this Type type, string name) - { - return type.GetMethod(name, DefaultFlags); - } - - public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags) - { - return type.GetTypeInfo().GetDeclaredMethod(name); - } - - public static MethodInfo GetMethod(this Type type, IList parameterTypes) - { - return type.GetMethod(null, parameterTypes); - } - - public static MethodInfo GetMethod(this Type type, string name, IList parameterTypes) - { - return type.GetMethod(name, DefaultFlags, null, parameterTypes, null); - } - - public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags, object placeHolder1, IList parameterTypes, object placeHolder2) - { - return type.GetTypeInfo().DeclaredMethods.Where(m => - { - if (name != null && m.Name != name) - return false; - - if (!TestAccessibility(m, bindingFlags)) - return false; - - return m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes); - }).SingleOrDefault(); - } - - public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingFlags, object placeholder1, Type propertyType, IList indexParameters, object placeholder2) - { - return type.GetTypeInfo().DeclaredProperties.Where(p => - { - if (name != null && name != p.Name) - return false; - if (propertyType != null && propertyType != p.PropertyType) - return false; - if (indexParameters != null) + public static bool IsDefined(this Type type, Type attributeType, bool inherit) { - if (!p.GetIndexParameters().Select(ip => ip.ParameterType).SequenceEqual(indexParameters)) + return type.GetTypeInfo().CustomAttributes.Any(a => a.AttributeType == attributeType); + } + + public static MethodInfo GetMethod(this Type type, string name) + { + return type.GetMethod(name, DefaultFlags); + } + + public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags) + { + return type.GetTypeInfo().GetDeclaredMethod(name); + } + + public static MethodInfo GetMethod(this Type type, IList parameterTypes) + { + return type.GetMethod(null, parameterTypes); + } + + public static MethodInfo GetMethod(this Type type, string name, IList parameterTypes) + { + return type.GetMethod(name, DefaultFlags, null, parameterTypes, null); + } + + public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags, object placeHolder1, IList parameterTypes, object placeHolder2) + { + return type.GetTypeInfo().DeclaredMethods.Where(m => + { + if (name != null && m.Name != name) + return false; + + if (!TestAccessibility(m, bindingFlags)) + return false; + + return m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes); + }).SingleOrDefault(); + } + + public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingFlags, object placeholder1, Type propertyType, IList indexParameters, object placeholder2) + { + return type.GetTypeInfo().DeclaredProperties.Where(p => + { + if (name != null && name != p.Name) + return false; + if (propertyType != null && propertyType != p.PropertyType) + return false; + if (indexParameters != null) + { + if (!p.GetIndexParameters().Select(ip => ip.ParameterType).SequenceEqual(indexParameters)) + return false; + } + + return true; + }).SingleOrDefault(); + } + + public static IEnumerable GetMember(this Type type, string name, MemberTypes memberType, BindingFlags bindingFlags) + { + return type.GetTypeInfo().GetMembersRecursive().Where(m => + { + if (name != null && name != m.Name) + return false; + if (m.MemberType() != memberType) + return false; + if (!TestAccessibility(m, bindingFlags)) + return false; + + return true; + }); + } + + public static IEnumerable GetConstructors(this Type type) + { + return type.GetConstructors(DefaultFlags); + } + + public static IEnumerable GetConstructors(this Type type, BindingFlags bindingFlags) + { + return type.GetConstructors(bindingFlags, null); + } + + private static IEnumerable GetConstructors(this Type type, BindingFlags bindingFlags, IList parameterTypes) + { + return type.GetTypeInfo().DeclaredConstructors.Where(c => + { + if (!TestAccessibility(c, bindingFlags)) + return false; + + if (parameterTypes != null && !c.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes)) + return false; + + return true; + }); + } + + public static ConstructorInfo GetConstructor(this Type type, IList parameterTypes) + { + return type.GetConstructor(DefaultFlags, null, parameterTypes, null); + } + + public static ConstructorInfo GetConstructor(this Type type, BindingFlags bindingFlags, object placeholder1, IList parameterTypes, object placeholder2) + { + return type.GetConstructors(bindingFlags, parameterTypes).SingleOrDefault(); + } + + public static MemberInfo[] GetMember(this Type type, string member) + { + return type.GetMember(member, DefaultFlags); + } + + public static MemberInfo[] GetMember(this Type type, string member, BindingFlags bindingFlags) + { + return type.GetTypeInfo().GetMembersRecursive().Where(m => m.Name == member && TestAccessibility(m, bindingFlags)).ToArray(); + } + + public static MemberInfo GetField(this Type type, string member) + { + return type.GetField(member, DefaultFlags); + } + + public static MemberInfo GetField(this Type type, string member, BindingFlags bindingFlags) + { + return type.GetTypeInfo().GetDeclaredField(member); + } + + public static IEnumerable GetProperties(this Type type, BindingFlags bindingFlags) + { + IList properties = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly)) + ? type.GetTypeInfo().DeclaredProperties.ToList() + : type.GetTypeInfo().GetPropertiesRecursive(); + + return properties.Where(p => TestAccessibility(p, bindingFlags)); + } + + private static IList GetMembersRecursive(this TypeInfo type) + { + TypeInfo t = type; + IList members = new List(); + while (t != null) + { + foreach (var member in t.DeclaredMembers) + { + if (!members.Any(p => p.Name == member.Name)) + members.Add(member); + } + t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null; + } + + return members; + } + + private static IList GetPropertiesRecursive(this TypeInfo type) + { + TypeInfo t = type; + IList properties = new List(); + while (t != null) + { + foreach (var member in t.DeclaredProperties) + { + if (!properties.Any(p => p.Name == member.Name)) + properties.Add(member); + } + t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null; + } + + return properties; + } + + private static IList GetFieldsRecursive(this TypeInfo type) + { + TypeInfo t = type; + IList fields = new List(); + while (t != null) + { + foreach (var member in t.DeclaredFields) + { + if (!fields.Any(p => p.Name == member.Name)) + fields.Add(member); + } + t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null; + } + + return fields; + } + + public static IEnumerable GetMethods(this Type type, BindingFlags bindingFlags) + { + return type.GetTypeInfo().DeclaredMethods; + } + + public static PropertyInfo GetProperty(this Type type, string name) + { + return type.GetProperty(name, DefaultFlags); + } + + public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingFlags) + { + return type.GetTypeInfo().GetDeclaredProperty(name); + } + + public static IEnumerable GetFields(this Type type) + { + return type.GetFields(DefaultFlags); + } + + public static IEnumerable GetFields(this Type type, BindingFlags bindingFlags) + { + IList fields = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly)) + ? type.GetTypeInfo().DeclaredFields.ToList() + : type.GetTypeInfo().GetFieldsRecursive(); + + return fields.Where(f => TestAccessibility(f, bindingFlags)).ToList(); + } + + private static bool TestAccessibility(PropertyInfo member, BindingFlags bindingFlags) + { + if (member.GetMethod != null && TestAccessibility(member.GetMethod, bindingFlags)) + return true; + + if (member.SetMethod != null && TestAccessibility(member.SetMethod, bindingFlags)) + return true; + return false; } - return true; - }).SingleOrDefault(); - } - - public static IEnumerable GetMember(this Type type, string name, MemberTypes memberType, BindingFlags bindingFlags) - { - return type.GetTypeInfo().GetMembersRecursive().Where(m => - { - if (name != null && name != m.Name) - return false; - if (m.MemberType() != memberType) - return false; - if (!TestAccessibility(m, bindingFlags)) - return false; - - return true; - }); - } - - public static IEnumerable GetConstructors(this Type type) - { - return type.GetConstructors(DefaultFlags); - } - - public static IEnumerable GetConstructors(this Type type, BindingFlags bindingFlags) - { - return type.GetConstructors(bindingFlags, null); - } - - private static IEnumerable GetConstructors(this Type type, BindingFlags bindingFlags, IList parameterTypes) - { - return type.GetTypeInfo().DeclaredConstructors.Where(c => - { - if (!TestAccessibility(c, bindingFlags)) - return false; - - if (parameterTypes != null && !c.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes)) - return false; - - return true; - }); - } - - public static ConstructorInfo GetConstructor(this Type type, IList parameterTypes) - { - return type.GetConstructor(DefaultFlags, null, parameterTypes, null); - } - - public static ConstructorInfo GetConstructor(this Type type, BindingFlags bindingFlags, object placeholder1, IList parameterTypes, object placeholder2) - { - return type.GetConstructors(bindingFlags, parameterTypes).SingleOrDefault(); - } - - public static MemberInfo[] GetMember(this Type type, string member) - { - return type.GetMember(member, DefaultFlags); - } - - public static MemberInfo[] GetMember(this Type type, string member, BindingFlags bindingFlags) - { - return type.GetTypeInfo().GetMembersRecursive().Where(m => m.Name == member && TestAccessibility(m, bindingFlags)).ToArray(); - } - - public static MemberInfo GetField(this Type type, string member) - { - return type.GetField(member, DefaultFlags); - } - - public static MemberInfo GetField(this Type type, string member, BindingFlags bindingFlags) - { - return type.GetTypeInfo().GetDeclaredField(member); - } - - public static IEnumerable GetProperties(this Type type, BindingFlags bindingFlags) - { - IList properties = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly)) - ? type.GetTypeInfo().DeclaredProperties.ToList() - : type.GetTypeInfo().GetPropertiesRecursive(); - - return properties.Where(p => TestAccessibility(p, bindingFlags)); - } - - private static IList GetMembersRecursive(this TypeInfo type) - { - TypeInfo t = type; - IList members = new List(); - while (t != null) - { - foreach (var member in t.DeclaredMembers) + private static bool TestAccessibility(MemberInfo member, BindingFlags bindingFlags) { - if (!members.Any(p => p.Name == member.Name)) - members.Add(member); + if (member is FieldInfo) + { + return TestAccessibility((FieldInfo) member, bindingFlags); + } + else if (member is MethodBase) + { + return TestAccessibility((MethodBase) member, bindingFlags); + } + else if (member is PropertyInfo) + { + return TestAccessibility((PropertyInfo) member, bindingFlags); + } + + throw new Exception("Unexpected member type."); } - t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null; - } - return members; - } - - private static IList GetPropertiesRecursive(this TypeInfo type) - { - TypeInfo t = type; - IList properties = new List(); - while (t != null) - { - foreach (var member in t.DeclaredProperties) + private static bool TestAccessibility(FieldInfo member, BindingFlags bindingFlags) { - if (!properties.Any(p => p.Name == member.Name)) - properties.Add(member); + bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) || + (!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic)); + + bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) || + (!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance)); + + return visibility && instance; } - t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null; - } - return properties; - } - - private static IList GetFieldsRecursive(this TypeInfo type) - { - TypeInfo t = type; - IList fields = new List(); - while (t != null) - { - foreach (var member in t.DeclaredFields) + private static bool TestAccessibility(MethodBase member, BindingFlags bindingFlags) { - if (!fields.Any(p => p.Name == member.Name)) - fields.Add(member); + bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) || + (!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic)); + + bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) || + (!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance)); + + return visibility && instance; } - t = (t.BaseType != null) ? t.BaseType.GetTypeInfo() : null; - } - return fields; - } + public static Type[] GetGenericArguments(this Type type) + { + return type.GetTypeInfo().GenericTypeArguments; + } - public static IEnumerable GetMethods(this Type type, BindingFlags bindingFlags) - { - return type.GetTypeInfo().DeclaredMethods; - } + public static IEnumerable GetInterfaces(this Type type) + { + return type.GetTypeInfo().ImplementedInterfaces; + } - public static PropertyInfo GetProperty(this Type type, string name) - { - return type.GetProperty(name, DefaultFlags); - } - - public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingFlags) - { - return type.GetTypeInfo().GetDeclaredProperty(name); - } - - public static IEnumerable GetFields(this Type type) - { - return type.GetFields(DefaultFlags); - } - - public static IEnumerable GetFields(this Type type, BindingFlags bindingFlags) - { - IList fields = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly)) - ? type.GetTypeInfo().DeclaredFields.ToList() - : type.GetTypeInfo().GetFieldsRecursive(); - - return fields.Where(f => TestAccessibility(f, bindingFlags)).ToList(); - } - - private static bool TestAccessibility(PropertyInfo member, BindingFlags bindingFlags) - { - if (member.GetMethod != null && TestAccessibility(member.GetMethod, bindingFlags)) - return true; - - if (member.SetMethod != null && TestAccessibility(member.SetMethod, bindingFlags)) - return true; - - return false; - } - - private static bool TestAccessibility(MemberInfo member, BindingFlags bindingFlags) - { - if (member is FieldInfo) - { - return TestAccessibility((FieldInfo)member, bindingFlags); - } - else if (member is MethodBase) - { - return TestAccessibility((MethodBase)member, bindingFlags); - } - else if (member is PropertyInfo) - { - return TestAccessibility((PropertyInfo)member, bindingFlags); - } - - throw new Exception("Unexpected member type."); - } - - private static bool TestAccessibility(FieldInfo member, BindingFlags bindingFlags) - { - bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) || - (!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic)); - - bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) || - (!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance)); - - return visibility && instance; - } - - private static bool TestAccessibility(MethodBase member, BindingFlags bindingFlags) - { - bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) || - (!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic)); - - bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) || - (!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance)); - - return visibility && instance; - } - - public static Type[] GetGenericArguments(this Type type) - { - return type.GetTypeInfo().GenericTypeArguments; - } - - public static IEnumerable GetInterfaces(this Type type) - { - return type.GetTypeInfo().ImplementedInterfaces; - } - - public static IEnumerable GetMethods(this Type type) - { - return type.GetTypeInfo().DeclaredMethods; - } + public static IEnumerable GetMethods(this Type type) + { + return type.GetTypeInfo().DeclaredMethods; + } #endif public static bool IsAbstract(this Type type) @@ -508,7 +516,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsAbstract; #else - return type.GetTypeInfo().IsAbstract; + return type.GetTypeInfo().IsAbstract; #endif } @@ -517,7 +525,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsVisible; #else - return type.GetTypeInfo().IsVisible; + return type.GetTypeInfo().IsVisible; #endif } @@ -526,7 +534,7 @@ namespace Newtonsoft.Json.Utilities #if !(NETFX_CORE || PORTABLE) return type.IsValueType; #else - return type.GetTypeInfo().IsValueType; + return type.GetTypeInfo().IsValueType; #endif } @@ -609,8 +617,8 @@ namespace Newtonsoft.Json.Utilities allTypes.Add(target); return from type in allTypes - from method in type.GetMethods() - select method; + from method in type.GetMethods() + select method; } } } \ No newline at end of file