[C#] Remove .NET 4.5 reflection shim

The reflection shim adapting across .NET 4.0 and .NET 4.5 is no longer
needed now that support for .NET v4.0 has been removed.
This commit is contained in:
Chad Walters 2017-06-27 15:06:14 -07:00 коммит произвёл Christopher Warrington
Родитель 3bbe401069
Коммит 31211e1070
14 изменённых файлов: 47 добавлений и 102 удалений

Просмотреть файл

@ -5,6 +5,7 @@ namespace Bond
{
using System;
using System.Linq.Expressions;
using System.Reflection;
using Bond.Expressions;
using Bond.Internal.Reflection;
@ -38,7 +39,7 @@ namespace Bond
else if (typeof(T).IsBonded())
{
create = Expression.Field(null,
typeof(Bonded<>).MakeGenericType(typeof(T).GetValueType()).GetField("Empty"));
typeof(Bonded<>).MakeGenericType(typeof(T).GetValueType()).GetTypeInfo().GetDeclaredField("Empty"));
}
else if (typeof(T).IsClass())
{

Просмотреть файл

@ -43,13 +43,13 @@ namespace Bond
/// </summary>
public static IEnumerable<ISchemaField> GetSchemaFields(this Type type)
{
var fields = from fieldInfo in type.GetDeclaredFields().Where(f => f.IsPublic)
var fields = from fieldInfo in type.GetTypeInfo().DeclaredFields.Where(f => f.IsPublic)
let idAttr = fieldInfo.GetAttribute<IdAttribute>()
where idAttr != null
select new Field(fieldInfo, idAttr.Value) as ISchemaField;
var properties =
from propertyInfo in type.GetDeclaredProperties()
from propertyInfo in type.GetTypeInfo().DeclaredProperties
let idAttr = propertyInfo.GetAttribute<IdAttribute>()
where idAttr != null
select new Property(propertyInfo, idAttr.Value) as ISchemaField;
@ -64,7 +64,7 @@ namespace Bond
public static Type GetValueType(this Type type)
{
if (type.IsBondNullable() || type.IsBonded())
return type.GetGenericArguments()[0];
return type.GetTypeInfo().GenericTypeArguments[0];
if (type.IsArray)
return type.GetElementType();
@ -74,6 +74,7 @@ namespace Bond
return type.GetMethod(typeof(IEnumerable<>), "GetEnumerator")
.ReturnType
.GetTypeInfo()
.GetDeclaredProperty("Current")
.PropertyType;
}
@ -83,7 +84,7 @@ namespace Bond
/// </summary>
public static KeyValuePair<Type, Type> GetKeyValueType(this Type type)
{
var types = GetValueType(type).GetGenericArguments();
var types = GetValueType(type).GetTypeInfo().GenericTypeArguments;
if (types.Length != 2)
{
@ -180,7 +181,7 @@ namespace Bond
if (!type.IsGenericType())
return false;
return typeof(ISet<>).MakeGenericType(type.GetGenericArguments()[0]).IsAssignableFrom(type);
return typeof(ISet<>).MakeGenericType(type.GetTypeInfo().GenericTypeArguments[0]).IsAssignableFrom(type);
}
/// <summary>
@ -309,14 +310,14 @@ namespace Bond
{
// Get all base interfaces. In case if an inheritance chain longer than 2, this returns all
// the base interfaces flattened in no particular order, so we have to find the direct parent.
var baseInterfaces = type.GetInterfaces()
var baseInterfaces = type.GetTypeInfo().ImplementedInterfaces
.Where(t => t.GetAttribute<SchemaAttribute>() != null).ToArray();
for (var i = 0; i < baseInterfaces.Length; i++)
{
var baseInterface = baseInterfaces[i];
var indirectBaseInterfacesCount =
baseInterface.GetInterfaces()
baseInterface.GetTypeInfo().ImplementedInterfaces
.Count(t => t.GetAttribute<SchemaAttribute>() != null);
if (indirectBaseInterfacesCount == baseInterfaces.Length - 1)
@ -641,7 +642,7 @@ namespace Bond
if (!type.IsGenericType())
return name;
var args = type.GetGenericArguments();
var args = type.GetTypeInfo().GenericTypeArguments;
var builder = new StringBuilder(name, args.Length * 64);
builder.Append("<");
@ -710,12 +711,12 @@ namespace Bond
}
else
{
memberTypeArguments = memberType.GetGenericArguments();
memberTypeArguments = memberType.GetTypeInfo().GenericTypeArguments;
}
return schemaGenericType.MakeGenericType(Enumerable.Zip(
memberTypeArguments,
schemaType.GetGenericArguments(),
schemaType.GetTypeInfo().GenericTypeArguments,
ResolveTypeArgumentTags).ToArray());
}
@ -739,14 +740,14 @@ namespace Bond
if (schemaType.IsGenericType())
{
return schemaType.GetGenericTypeDefinition().MakeGenericType(
schemaType.GetGenericArguments().Select(type =>
schemaType.GetTypeInfo().GenericTypeArguments.Select(type =>
{
if (type.IsGenericType())
{
var genericType = type.GetGenericTypeDefinition();
if (genericType == typeof(Tag.nullable<>))
{
var nullableValue = type.GetGenericArguments()[0];
var nullableValue = type.GetTypeInfo().GenericTypeArguments[0];
return nullableValue.IsClass() || nullableValue.IsBondBlob()
? nullableValue.GetObjectType()
: typeof(Nullable<>).MakeGenericType(nullableValue.GetObjectType());
@ -754,7 +755,7 @@ namespace Bond
if (genericType == typeof(Tag.bonded<>))
{
return typeof(IBonded<>).MakeGenericType(type.GetGenericArguments()[0].GetObjectType());
return typeof(IBonded<>).MakeGenericType(type.GetTypeInfo().GenericTypeArguments[0].GetObjectType());
}
}

Просмотреть файл

@ -141,8 +141,8 @@ namespace Bond
else
{
var schemaT = typeof (Schema<>).MakeGenericType(type);
var metadataProp = schemaT.GetDeclaredProperty("Metadata");
var fieldsProp = schemaT.GetDeclaredProperty("Fields");
var metadataProp = schemaT.GetTypeInfo().GetDeclaredProperty("Metadata");
var fieldsProp = schemaT.GetTypeInfo().GetDeclaredProperty("Fields");
typeDef.struct_def = GetStructDef(
type,
metadataProp.GetValue(null) as Metadata,

Просмотреть файл

@ -4,7 +4,7 @@
namespace Bond.Expressions
{
using System.Linq.Expressions;
using Bond.Internal.Reflection;
using System.Reflection;
internal static class DataExpression
{
@ -12,10 +12,10 @@ namespace Bond.Expressions
// in the type, ignoring inherited members.
public static MemberExpression PropertyOrField(Expression expression, string name)
{
var property = expression.Type.GetDeclaredProperty(name);
var property = expression.Type.GetTypeInfo().GetDeclaredProperty(name);
return (property != null) ?
Expression.Property(expression, property) :
Expression.Field(expression, expression.Type.GetField(name));
Expression.Field(expression, expression.Type.GetTypeInfo().GetDeclaredField(name));
}
}
}

Просмотреть файл

@ -558,7 +558,7 @@ namespace Bond.Expressions
}
else if (schemaType.IsGenericType())
{
schemaType = schemaType.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments());
schemaType = schemaType.GetGenericTypeDefinition().MakeGenericType(type.GetTypeInfo().GenericTypeArguments);
}
else if (schemaType.IsArray)
{

Просмотреть файл

@ -161,7 +161,7 @@ namespace Bond.Expressions
if (value.Type.IsGenericType())
{
if (typeof(IList<>).MakeGenericType(value.Type.GetGenericArguments()[0]).IsAssignableFrom(value.Type))
if (typeof(IList<>).MakeGenericType(value.Type.GetTypeInfo().GenericTypeArguments[0]).IsAssignableFrom(value.Type))
return ListContainer(itemHandler);
if (typeof(LinkedList<>) == value.Type.GetGenericTypeDefinition())
@ -301,7 +301,7 @@ namespace Bond.Expressions
{
Debug.Assert(schemaType.IsBondContainer());
var nodeType = typeof(LinkedListNode<>).MakeGenericType(value.Type.GetGenericArguments()[0]);
var nodeType = typeof(LinkedListNode<>).MakeGenericType(value.Type.GetTypeInfo().GenericTypeArguments[0]);
var node = Expression.Variable(nodeType, "node");
var item = Expression.Property(node, "Value");
var next = Expression.NotEqual(

Просмотреть файл

@ -6,6 +6,7 @@ namespace Bond.Expressions
using System;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using Bond.Protocols;
using Bond.Internal.Reflection;
@ -62,7 +63,7 @@ namespace Bond.Expressions
else
{
var genericParserType = attribute.ParserType;
if (!genericParserType.IsGenericType() || genericParserType.GetGenericParameters().Length != 1)
if (!genericParserType.IsGenericType() || genericParserType.GetTypeInfo().GenericTypeParameters.Length != 1)
{
throw new InvalidOperationException(
"Parser type is expected to be a generic type with one type param for Reader.");

Просмотреть файл

@ -6,6 +6,7 @@ namespace Bond.Expressions
using System;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using Bond.Internal.Reflection;
internal static class SerializerGeneratorFactory<R, W>
@ -33,7 +34,7 @@ namespace Bond.Expressions
}
else
{
if (!attribute.Type.IsGenericType() || attribute.Type.GetGenericParameters().Length != 2)
if (!attribute.Type.IsGenericType() || attribute.Type.GetTypeInfo().GenericTypeParameters.Length != 2)
{
throw new InvalidOperationException(
"Serializer generator is expected to be a generic type with two type parameters.");

Просмотреть файл

@ -7,6 +7,7 @@ namespace Bond.Expressions
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Bond.Internal.Reflection;
internal class TypeAlias
@ -28,7 +29,7 @@ namespace Bond.Expressions
leftType.IsGenericType() &&
leftType.GetGenericTypeDefinition() == typeof (Nullable<>))
{
leftType = leftType.GetGenericArguments()[0];
leftType = leftType.GetTypeInfo().GenericTypeArguments[0];
}
var value = Convert(right, leftType);
@ -48,7 +49,7 @@ namespace Bond.Expressions
value.Type.IsGenericType() &&
value.Type.GetGenericTypeDefinition() == typeof (Nullable<>))
{
value = Expression.Convert(value, value.Type.GetGenericArguments()[0]);
value = Expression.Convert(value, value.Type.GetTypeInfo().GenericTypeArguments[0]);
}
if (type != value.Type)

Просмотреть файл

@ -54,7 +54,7 @@ namespace Bond.Internal.Reflection
public static MethodInfo FindMethod(this Type type, string name, params Type[] paramTypes)
{
var methods = type.GetDeclaredMethods(name);
var methods = type.GetTypeInfo().GetDeclaredMethods(name);
var result = (
from method in methods
@ -67,7 +67,7 @@ namespace Bond.Internal.Reflection
{
if (type.IsInterface())
{
var interfaces = type.GetInterfaces();
var interfaces = type.GetTypeInfo().ImplementedInterfaces;
var matchedMethods = interfaces.Select(x => x.FindMethod(name, paramTypes)).Where(x => x != null).ToList();
if (matchedMethods.Count > 1)
@ -92,7 +92,7 @@ namespace Bond.Internal.Reflection
public static MethodInfo ResolveMethod(this Type type, string name, params Type[] argumentTypes)
{
var methods = type.GetDeclaredMethods(name);
var methods = type.GetTypeInfo().GetDeclaredMethods(name);
var typeArgs = new Type[0];
foreach (var method in methods)
@ -112,9 +112,9 @@ namespace Bond.Internal.Reflection
if (param.IsGenericType() && arg.IsGenericType() &&
param.GetGenericTypeDefinition() == arg.GetGenericTypeDefinition() &&
param.GetGenericArguments().All(p => p.IsGenericParameter))
param.GetTypeInfo().GenericTypeArguments.All(p => p.IsGenericParameter))
{
typeArgs = arg.GetGenericArguments();
typeArgs = arg.GetTypeInfo().GenericTypeArguments;
continue;
}
break;
@ -133,7 +133,7 @@ namespace Bond.Internal.Reflection
public static ConstructorInfo GetConstructor(this Type type, params Type[] paramTypes)
{
var methods = type.GetDeclaredConstructors();
var methods = type.GetTypeInfo().DeclaredConstructors;
return (
from method in methods
@ -146,7 +146,7 @@ namespace Bond.Internal.Reflection
public static PropertyInfo GetDeclaredProperty(this Type type, string name, Type returnType)
{
var property = type.GetDeclaredProperty(name);
var property = type.GetTypeInfo().GetDeclaredProperty(name);
return (property != null && property.PropertyType == returnType) ? property : null;
}
@ -157,13 +157,13 @@ namespace Bond.Internal.Reflection
static Type MakeGenericTypeFrom(this Type genericType, Type concreteType)
{
var typeArguments = concreteType.GetGenericArguments();
var typeArguments = concreteType.GetTypeInfo().GenericTypeArguments;
if (concreteType.IsArray)
{
typeArguments = new[] { concreteType.GetElementType() };
}
var typeParameters = genericType.GetGenericParameters();
var typeParameters = genericType.GetTypeInfo().GenericTypeParameters;
if (typeArguments.Length == 2 && typeParameters.Length == 1)
typeArguments = new[] { typeof(KeyValuePair<,>).MakeGenericType(typeArguments) };

Просмотреть файл

@ -1,58 +0,0 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Bond.Internal.Reflection
{
using System;
using System.Collections.Generic;
using System.Reflection;
// Common reflection interface implementation for .NET 4.5 Portable Profile
internal static class Reflection45
{
public static Type[] GetGenericParameters(this Type type)
{
return type.GetTypeInfo().GenericTypeParameters;
}
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<ConstructorInfo> GetDeclaredConstructors(this Type type)
{
return type.GetTypeInfo().DeclaredConstructors;
}
public static FieldInfo GetField(this Type type, string name)
{
return type.GetTypeInfo().GetDeclaredField(name);
}
public static IEnumerable<FieldInfo> GetDeclaredFields(this Type type)
{
return type.GetTypeInfo().DeclaredFields;
}
public static IEnumerable<PropertyInfo> GetDeclaredProperties(this Type type)
{
return type.GetTypeInfo().DeclaredProperties;
}
public static PropertyInfo GetDeclaredProperty(this Type type, string name)
{
return type.GetTypeInfo().GetDeclaredProperty(name);
}
public static IEnumerable<MethodInfo> GetDeclaredMethods(this Type type, string name)
{
return type.GetTypeInfo().GetDeclaredMethods(name);
}
}
}

Просмотреть файл

@ -13,7 +13,6 @@
<ItemGroup>
<Compile Include="properties\AssemblyInfo.cs" />
<Compile Include="Reflection.cs" />
<Compile Include="Reflection45.cs" Condition="'$(TargetFrameworkVersion)' == 'v4.5'" />
</ItemGroup>
<Import Project="$(MSBuildThisFileDirectory)\..\..\build\internal\Common.Internal.targets" />
</Project>

Просмотреть файл

@ -1,8 +1,8 @@
namespace UnitTest
{
using System.Collections.Generic;
using System.Reflection;
using Bond;
using Bond.Internal.Reflection;
[Schema, Attribute("xmlns", "urn:UnitTest.BondClass")]
class BondClass<T>
@ -17,7 +17,7 @@
public override bool Equals(object that)
{
var thatField = that.GetType().GetField("field");
var thatField = that.GetType().GetTypeInfo().GetDeclaredField("field");
return field.IsEqual<object, object>(thatField.GetValue(that));
}
@ -40,7 +40,7 @@
{
if (that is BondClass<T2>)
{
var thatField = that.GetType().GetField("field");
var thatField = that.GetType().GetTypeInfo().GetDeclaredField("field");
return field.IsEqual<object, object>(thatField.GetValue(that));
}

Просмотреть файл

@ -289,11 +289,10 @@ namespace UnitTest
if (typeDefinition == typeof(CustomBonded<>))
{
var arg = arguments[0]; // CustomBondedVoid<R>
#if NETCOREAPP1_0
Type[] genericArgs = Bond.Internal.Reflection.Reflection45.GetGenericArguments(type);
#else
Type[] genericArgs = type.GetGenericArguments();
#endif
Type[] genericArgs = type.GetTypeInfo().GenericTypeArguments;
Assert.IsNotEmpty(genericArgs);
var bondedConvert = typeof(IBonded).GetMethod("Convert").MakeGenericMethod(genericArgs);
return Expression.ConvertChecked(Expression.Call(arg, bondedConvert), type);