-WinRT build

This commit is contained in:
James Newton-King
2013-12-08 13:37:13 +13:00
parent 7e9824c5f6
commit c7fd4d5e29
8 changed files with 390 additions and 383 deletions
@@ -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
{
@@ -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
{
@@ -142,9 +142,11 @@
<Compile Include="Linq\JArrayTests.cs" />
<Compile Include="Linq\JConstructorTests.cs" />
<Compile Include="Linq\JObjectTests.cs" />
<Compile Include="Linq\JPathTests.cs" />
<Compile Include="Linq\JPropertyTests.cs" />
<Compile Include="Linq\JRawTests.cs" />
<Compile Include="Linq\JsonPath\JPathExecuteTests.cs" />
<Compile Include="Linq\JsonPath\JPathParseTests.cs" />
<Compile Include="Linq\JsonPath\QueryExpressionTests.cs" />
<Compile Include="Linq\JTokenEqualityComparerTests.cs" />
<Compile Include="Linq\JTokenReaderTest.cs" />
<Compile Include="Linq\JTokenTests.cs" />
@@ -177,7 +179,6 @@
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\TypeNameHandlingTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
<Compile Include="SilverlightTests.cs" />
<Compile Include="TestFixtureBase.cs" />
<Compile Include="TestObjects\AbstractGenericBase.cs" />
<Compile Include="TestObjects\ArgumentConverterPrecedenceClassConverter.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<Foo2>(reader1);
Assert.AreEqual("bar", foo1.Name);
var reader2 = new JsonTextReader(sr);
var serializer2 = new JsonSerializer { };
var foo2 = serializer2.Deserialize<Foo2>(reader2);
Assert.AreEqual("baz", foo2.Name);
}
#if !(PORTABLE || PORTABLE40 || NETFX_CORE)
[Test]
public void SerializeDictionaryWithStructKey()
@@ -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);
@@ -127,6 +127,7 @@
<Compile Include="Converters\DataSetConverter.cs" />
<Compile Include="Converters\DataTableConverter.cs" />
<Compile Include="Converters\DateTimeConverterBase.cs" />
<Compile Include="Converters\DiscriminatedUnionConverter.cs" />
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
<Compile Include="Converters\ExpandoObjectConverter.cs" />
<Compile Include="Converters\IsoDateTimeConverter.cs" />
@@ -178,11 +179,19 @@
<Compile Include="Linq\JContainer.cs" />
<Compile Include="Linq\JEnumerable.cs" />
<Compile Include="Linq\JObject.cs" />
<Compile Include="Linq\JPath.cs" />
<Compile Include="Linq\JProperty.cs" />
<Compile Include="Linq\JPropertyDescriptor.cs" />
<Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="Linq\JRaw.cs" />
<Compile Include="Linq\JsonPath\ArrayIndexFilter.cs" />
<Compile Include="Linq\JsonPath\ArrayMultipleIndexFilter.cs" />
<Compile Include="Linq\JsonPath\ArraySliceFilter.cs" />
<Compile Include="Linq\JsonPath\FieldFilter.cs" />
<Compile Include="Linq\JsonPath\JPath.cs" />
<Compile Include="Linq\JsonPath\PathFilter.cs" />
<Compile Include="Linq\JsonPath\QueryExpression.cs" />
<Compile Include="Linq\JsonPath\QueryFilter.cs" />
<Compile Include="Linq\JsonPath\ScanFilter.cs" />
<Compile Include="Linq\JToken.cs" />
<Compile Include="Linq\JTokenEqualityComparer.cs" />
<Compile Include="Linq\JTokenReader.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<T>(object provider, bool inherit) where T : Attribute
{
if (provider is Type)
return ((Type)provider).GetTypeInfo().GetCustomAttributes<T>(inherit).ToArray();
public static T[] GetAttributes<T>(object provider, bool inherit) where T : Attribute
{
if (provider is Type)
return ((Type) provider).GetTypeInfo().GetCustomAttributes<T>(inherit).ToArray();
if (provider is Assembly)
return ((Assembly)provider).GetCustomAttributes<T>().ToArray();
if (provider is Assembly)
return ((Assembly) provider).GetCustomAttributes<T>().ToArray();
if (provider is MemberInfo)
return ((MemberInfo)provider).GetCustomAttributes<T>(inherit).ToArray();
if (provider is MemberInfo)
return ((MemberInfo) provider).GetCustomAttributes<T>(inherit).ToArray();
if (provider is Module)
return ((Module)provider).GetCustomAttributes<T>().ToArray();
if (provider is Module)
return ((Module) provider).GetCustomAttributes<T>().ToArray();
if (provider is ParameterInfo)
return ((ParameterInfo)provider).GetCustomAttributes<T>(inherit).ToArray();
if (provider is ParameterInfo)
return ((ParameterInfo) provider).GetCustomAttributes<T>(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)
+323 -315
View File
@@ -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<Type> parameterTypes)
{
return type.GetMethod(null, parameterTypes);
}
public static MethodInfo GetMethod(this Type type, string name, IList<Type> parameterTypes)
{
return type.GetMethod(name, DefaultFlags, null, parameterTypes, null);
}
public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags, object placeHolder1, IList<Type> 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<Type> 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<Type> parameterTypes)
{
return type.GetMethod(null, parameterTypes);
}
public static MethodInfo GetMethod(this Type type, string name, IList<Type> parameterTypes)
{
return type.GetMethod(name, DefaultFlags, null, parameterTypes, null);
}
public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags, object placeHolder1, IList<Type> 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<Type> 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<MemberInfo> 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<ConstructorInfo> GetConstructors(this Type type)
{
return type.GetConstructors(DefaultFlags);
}
public static IEnumerable<ConstructorInfo> GetConstructors(this Type type, BindingFlags bindingFlags)
{
return type.GetConstructors(bindingFlags, null);
}
private static IEnumerable<ConstructorInfo> GetConstructors(this Type type, BindingFlags bindingFlags, IList<Type> 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<Type> parameterTypes)
{
return type.GetConstructor(DefaultFlags, null, parameterTypes, null);
}
public static ConstructorInfo GetConstructor(this Type type, BindingFlags bindingFlags, object placeholder1, IList<Type> 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<PropertyInfo> GetProperties(this Type type, BindingFlags bindingFlags)
{
IList<PropertyInfo> properties = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly))
? type.GetTypeInfo().DeclaredProperties.ToList()
: type.GetTypeInfo().GetPropertiesRecursive();
return properties.Where(p => TestAccessibility(p, bindingFlags));
}
private static IList<MemberInfo> GetMembersRecursive(this TypeInfo type)
{
TypeInfo t = type;
IList<MemberInfo> members = new List<MemberInfo>();
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<PropertyInfo> GetPropertiesRecursive(this TypeInfo type)
{
TypeInfo t = type;
IList<PropertyInfo> properties = new List<PropertyInfo>();
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<FieldInfo> GetFieldsRecursive(this TypeInfo type)
{
TypeInfo t = type;
IList<FieldInfo> fields = new List<FieldInfo>();
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<MethodInfo> 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<FieldInfo> GetFields(this Type type)
{
return type.GetFields(DefaultFlags);
}
public static IEnumerable<FieldInfo> GetFields(this Type type, BindingFlags bindingFlags)
{
IList<FieldInfo> 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<MemberInfo> 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<ConstructorInfo> GetConstructors(this Type type)
{
return type.GetConstructors(DefaultFlags);
}
public static IEnumerable<ConstructorInfo> GetConstructors(this Type type, BindingFlags bindingFlags)
{
return type.GetConstructors(bindingFlags, null);
}
private static IEnumerable<ConstructorInfo> GetConstructors(this Type type, BindingFlags bindingFlags, IList<Type> 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<Type> parameterTypes)
{
return type.GetConstructor(DefaultFlags, null, parameterTypes, null);
}
public static ConstructorInfo GetConstructor(this Type type, BindingFlags bindingFlags, object placeholder1, IList<Type> 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<PropertyInfo> GetProperties(this Type type, BindingFlags bindingFlags)
{
IList<PropertyInfo> properties = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly))
? type.GetTypeInfo().DeclaredProperties.ToList()
: type.GetTypeInfo().GetPropertiesRecursive();
return properties.Where(p => TestAccessibility(p, bindingFlags));
}
private static IList<MemberInfo> GetMembersRecursive(this TypeInfo type)
{
TypeInfo t = type;
IList<MemberInfo> members = new List<MemberInfo>();
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<PropertyInfo> GetPropertiesRecursive(this TypeInfo type)
{
TypeInfo t = type;
IList<PropertyInfo> properties = new List<PropertyInfo>();
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<FieldInfo> GetFieldsRecursive(this TypeInfo type)
{
TypeInfo t = type;
IList<FieldInfo> fields = new List<FieldInfo>();
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<MethodInfo> GetMethods(this Type type, BindingFlags bindingFlags)
{
return type.GetTypeInfo().DeclaredMethods;
}
public static IEnumerable<Type> 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<FieldInfo> GetFields(this Type type)
{
return type.GetFields(DefaultFlags);
}
public static IEnumerable<FieldInfo> GetFields(this Type type, BindingFlags bindingFlags)
{
IList<FieldInfo> 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<Type> GetInterfaces(this Type type)
{
return type.GetTypeInfo().ImplementedInterfaces;
}
public static IEnumerable<MethodInfo> GetMethods(this Type type)
{
return type.GetTypeInfo().DeclaredMethods;
}
public static IEnumerable<MethodInfo> 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;
}
}
}