Commit d1b2e154 authored by mgravell's avatar mgravell

get rid of all the NS1.3 fallback code

parent 2f7081a3
......@@ -37,7 +37,7 @@ public static partial class SqlMapperExtensions
var dynParms = new DynamicParameters();
dynParms.Add("@id", id);
if (!type.IsInterface())
if (!type.IsInterface)
return (await connection.QueryAsync<T>(sql, dynParms, transaction, commandTimeout).ConfigureAwait(false)).FirstOrDefault();
var res = (await connection.QueryAsync<dynamic>(sql, dynParms).ConfigureAwait(false)).FirstOrDefault() as IDictionary<string, object>;
......@@ -51,7 +51,7 @@ public static partial class SqlMapperExtensions
{
var val = res[property.Name];
if (val == null) continue;
if (property.PropertyType.IsGenericType() && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var genericType = Nullable.GetUnderlyingType(property.PropertyType);
if (genericType != null) property.SetValue(obj, Convert.ChangeType(val, genericType), null);
......@@ -92,7 +92,7 @@ public static partial class SqlMapperExtensions
GetQueries[cacheType.TypeHandle] = sql;
}
if (!type.IsInterface())
if (!type.IsInterface)
{
return connection.QueryAsync<T>(sql, null, transaction, commandTimeout);
}
......@@ -110,7 +110,7 @@ public static partial class SqlMapperExtensions
{
var val = res[property.Name];
if (val == null) continue;
if (property.PropertyType.IsGenericType() && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var genericType = Nullable.GetUnderlyingType(property.PropertyType);
if (genericType != null) property.SetValue(obj, Convert.ChangeType(val, genericType), null);
......@@ -148,11 +148,11 @@ public static partial class SqlMapperExtensions
isList = true;
type = type.GetElementType();
}
else if (type.IsGenericType())
else if (type.IsGenericType)
{
var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
......@@ -219,11 +219,11 @@ public static partial class SqlMapperExtensions
{
type = type.GetElementType();
}
else if (type.IsGenericType())
else if (type.IsGenericType)
{
var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
......@@ -288,11 +288,11 @@ public static partial class SqlMapperExtensions
{
type = type.GetElementType();
}
else if (type.IsGenericType())
else if (type.IsGenericType)
{
var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
......
......@@ -190,7 +190,7 @@ private static PropertyInfo GetSingleKey<T>(string method)
T obj;
if (type.IsInterface())
if (type.IsInterface)
{
var res = connection.Query(sql, dynParms).FirstOrDefault() as IDictionary<string, object>;
......@@ -203,7 +203,7 @@ private static PropertyInfo GetSingleKey<T>(string method)
{
var val = res[property.Name];
if (val == null) continue;
if (property.PropertyType.IsGenericType() && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var genericType = Nullable.GetUnderlyingType(property.PropertyType);
if (genericType != null) property.SetValue(obj, Convert.ChangeType(val, genericType), null);
......@@ -248,7 +248,7 @@ private static PropertyInfo GetSingleKey<T>(string method)
GetQueries[cacheType.TypeHandle] = sql;
}
if (!type.IsInterface()) return connection.Query<T>(sql, null, transaction, commandTimeout: commandTimeout);
if (!type.IsInterface) return connection.Query<T>(sql, null, transaction, commandTimeout: commandTimeout);
var result = connection.Query(sql);
var list = new List<T>();
......@@ -259,7 +259,7 @@ private static PropertyInfo GetSingleKey<T>(string method)
{
var val = res[property.Name];
if (val == null) continue;
if (property.PropertyType.IsGenericType() && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var genericType = Nullable.GetUnderlyingType(property.PropertyType);
if (genericType != null) property.SetValue(obj, Convert.ChangeType(val, genericType), null);
......@@ -307,7 +307,7 @@ private static string GetTableName(Type type)
else
{
name = type.Name + "s";
if (type.IsInterface() && name.StartsWith("I"))
if (type.IsInterface && name.StartsWith("I"))
name = name.Substring(1);
}
}
......@@ -336,11 +336,11 @@ private static string GetTableName(Type type)
isList = true;
type = type.GetElementType();
}
else if (type.IsGenericType())
else if (type.IsGenericType)
{
var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
......@@ -417,11 +417,11 @@ private static string GetTableName(Type type)
{
type = type.GetElementType();
}
else if (type.IsGenericType())
else if (type.IsGenericType)
{
var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
......@@ -486,11 +486,11 @@ private static string GetTableName(Type type)
{
type = type.GetElementType();
}
else if (type.IsGenericType())
else if (type.IsGenericType)
{
var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
......
......@@ -252,7 +252,7 @@ protected Action<TDatabase> CreateTableConstructor(params Type[] tableTypes)
var il = dm.GetILGenerator();
var setters = GetType().GetProperties()
.Where(p => p.PropertyType.IsGenericType() && tableTypes.Contains(p.PropertyType.GetGenericTypeDefinition()))
.Where(p => p.PropertyType.IsGenericType && tableTypes.Contains(p.PropertyType.GetGenericTypeDefinition()))
.Select(p => Tuple.Create(
p.GetSetMethod(true),
p.PropertyType.GetConstructor(new[] { typeof(TDatabase), typeof(string) }),
......
......@@ -91,8 +91,8 @@ private static List<PropertyInfo> RelevantProperties()
p.GetSetMethod(true) != null
&& p.GetGetMethod(true) != null
&& (p.PropertyType == typeof(string)
|| p.PropertyType.IsValueType()
|| (p.PropertyType.IsGenericType() && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))
|| p.PropertyType.IsValueType
|| (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))
).ToList();
}
......
......@@ -26,4 +26,5 @@
</PackageReference>
<PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
</ItemGroup>
<!-- note: define SQLCE if SQL CE is available -->
</Project>
......@@ -2,14 +2,10 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
using Dapper.Contrib.Extensions;
using Xunit;
#if !NETCOREAPP1_0 && !NETCOREAPP2_0
using System.Transactions;
using System.Data.SqlServerCe;
#endif
using FactAttribute = Dapper.Tests.Contrib.SkippableFactAttribute;
namespace Dapper.Tests.Contrib
......@@ -525,7 +521,7 @@ public void InsertGetUpdate()
}
}
#if !NETCOREAPP1_0 && !NETCOREAPP2_0
#if SQLCE
[Fact(Skip = "Not parallel friendly - thinking about how to test this")]
public void InsertWithCustomDbType()
{
......@@ -563,7 +559,7 @@ public void InsertWithCustomTableNameMapper()
{
SqlMapperExtensions.TableNameMapper = type =>
{
switch (type.Name())
switch (type.Name)
{
case "Person":
return "People";
......@@ -573,7 +569,7 @@ public void InsertWithCustomTableNameMapper()
return tableattr.Name;
var name = type.Name + "s";
if (type.IsInterface() && name.StartsWith("I"))
if (type.IsInterface && name.StartsWith("I"))
return name.Substring(1);
return name;
}
......@@ -653,7 +649,6 @@ public void Transactions()
}
}
#if !NETCOREAPP1_0 && !NETCOREAPP2_0
[Fact]
public void TransactionScope()
{
......@@ -665,11 +660,10 @@ public void TransactionScope()
txscope.Dispose(); //rollback
Assert.IsNull(connection.Get<Car>(id)); //returns null - car with that id should not exist
Assert.Null(connection.Get<Car>(id)); //returns null - car with that id should not exist
}
}
}
#endif
[Fact]
public void InsertCheckKey()
......
......@@ -7,10 +7,6 @@
using Xunit;
using Xunit.Sdk;
#if !NETCOREAPP1_0 && !NETCOREAPP2_0
using System.Data.SqlServerCe;
#endif
namespace Dapper.Tests.Contrib
{
// The test suites here implement TestSuiteBase so that each provider runs
......@@ -148,7 +144,8 @@ static SQLiteTestSuite()
}
}
#if !NETCOREAPP1_0 && !NETCOREAPP2_0
#if SQLCE
public class SqlCETestSuite : TestSuite
{
const string FileName = "Test.DB.sdf";
......
using System;
using System.Data;
#if !NETSTANDARD1_3
namespace Dapper
{
internal sealed class DataTableHandler : SqlMapper.ITypeHandler
......@@ -16,4 +15,3 @@ public void SetValue(IDbDataParameter parameter, object value)
}
}
}
#endif
\ No newline at end of file
......@@ -26,27 +26,11 @@ public DefaultTypeMap(Type type)
Properties = GetSettableProps(type);
_type = type;
}
#if NETSTANDARD1_3
private static bool IsParameterMatch(ParameterInfo[] x, ParameterInfo[] y)
{
if (ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
if (x.Length != y.Length) return false;
for (int i = 0; i < x.Length; i++)
if (x[i].ParameterType != y[i].ParameterType) return false;
return true;
}
#endif
internal static MethodInfo GetPropertySetter(PropertyInfo propertyInfo, Type type)
{
if (propertyInfo.DeclaringType == type) return propertyInfo.GetSetMethod(true);
#if NETSTANDARD1_3
return propertyInfo.DeclaringType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Single(x => x.Name == propertyInfo.Name
&& x.PropertyType == propertyInfo.PropertyType
&& IsParameterMatch(x.GetIndexParameters(), propertyInfo.GetIndexParameters())
).GetSetMethod(true);
#else
return propertyInfo.DeclaringType.GetProperty(
propertyInfo.Name,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
......@@ -54,7 +38,6 @@ internal static MethodInfo GetPropertySetter(PropertyInfo propertyInfo, Type typ
propertyInfo.PropertyType,
propertyInfo.GetIndexParameters().Select(p => p.ParameterType).ToArray(),
null).GetSetMethod(true);
#endif
}
internal static List<PropertyInfo> GetSettableProps(Type t)
......@@ -97,9 +80,9 @@ public ConstructorInfo FindConstructor(string[] names, Type[] types)
continue;
var unboxedType = Nullable.GetUnderlyingType(ctorParameters[i].ParameterType) ?? ctorParameters[i].ParameterType;
if ((unboxedType != types[i] && !SqlMapper.HasTypeHandler(unboxedType))
&& !(unboxedType.IsEnum() && Enum.GetUnderlyingType(unboxedType) == types[i])
&& !(unboxedType.IsEnum && Enum.GetUnderlyingType(unboxedType) == types[i])
&& !(unboxedType == typeof(char) && types[i] == typeof(string))
&& !(unboxedType.IsEnum() && types[i] == typeof(string)))
&& !(unboxedType.IsEnum && types[i] == typeof(string)))
{
break;
}
......@@ -118,11 +101,7 @@ public ConstructorInfo FindConstructor(string[] names, Type[] types)
public ConstructorInfo FindExplicitConstructor()
{
var constructors = _type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
#if NETSTANDARD1_3
var withAttr = constructors.Where(c => c.CustomAttributes.Any(x => x.AttributeType == typeof(ExplicitConstructorAttribute))).ToList();
#else
var withAttr = constructors.Where(c => c.GetCustomAttributes(typeof(ExplicitConstructorAttribute), true).Length > 0).ToList();
#endif
if (withAttr.Count == 1)
{
......
......@@ -6,10 +6,6 @@
using System.Reflection;
using System.Reflection.Emit;
#if NETSTANDARD1_3
using ApplicationException = System.InvalidOperationException;
#endif
namespace Dapper
{
/// <summary>
......
......@@ -5,9 +5,7 @@
namespace Dapper
{
internal sealed class SqlDataRecordHandler<T> : SqlMapper.ITypeHandler
#if !NETSTANDARD1_3
where T : IDataRecord
#endif
{
public object Parse(Type destinationType, object value)
{
......
......@@ -12,9 +12,7 @@ namespace Dapper
/// Used to pass a IEnumerable&lt;SqlDataRecord&gt; as a SqlDataRecordListTVPParameter
/// </summary>
internal sealed class SqlDataRecordListTVPParameter<T> : SqlMapper.ICustomQueryParameter
#if !NETSTANDARD1_3
where T : IDataRecord
#endif
{
private readonly IEnumerable<T> data;
private readonly string typeName;
......
#if !NETSTANDARD1_3 // needs the component-model API
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
......@@ -104,4 +103,3 @@ public override void SetValue(object component, object value)
}
}
}
#endif
......@@ -140,7 +140,7 @@ public static IEnumerable<dynamic> Parse(this IDataReader reader)
{
concreteType = concreteType ?? typeof(T);
var func = GetDeserializer(concreteType, reader, startIndex, length, returnNullIfFirstMissing);
if (concreteType.IsValueType())
if (concreteType.IsValueType)
{
return _ => (T)func(_);
}
......
......@@ -11,9 +11,7 @@ public static partial class SqlMapper
/// </summary>
/// <typeparam name="T">The type to have a cache for.</typeparam>
[Obsolete(ObsoleteInternalUsageOnly, false)]
#if !NETSTANDARD1_3
[Browsable(false)]
#endif
[EditorBrowsable(EditorBrowsableState.Never)]
public static class TypeHandlerCache<T>
{
......
This diff is collapsed.
using System.Data;
#if !NETSTANDARD1_3
namespace Dapper
{
/// <summary>
......@@ -49,4 +48,3 @@ internal static void Set(IDbDataParameter parameter, DataTable table, string typ
}
}
}
#endif
using System;
using System.Reflection;
using System.Collections.Generic;
namespace Dapper
{
internal static class TypeExtensions
{
public static string Name(this Type type) =>
#if NETSTANDARD1_3 || NETCOREAPP1_0
type.GetTypeInfo().Name;
#else
type.Name;
#endif
public static bool IsValueType(this Type type) =>
#if NETSTANDARD1_3 || NETCOREAPP1_0
type.GetTypeInfo().IsValueType;
#else
type.IsValueType;
#endif
public static bool IsEnum(this Type type) =>
#if NETSTANDARD1_3 || NETCOREAPP1_0
type.GetTypeInfo().IsEnum;
#else
type.IsEnum;
#endif
public static bool IsGenericType(this Type type) =>
#if NETSTANDARD1_3 || NETCOREAPP1_0
type.GetTypeInfo().IsGenericType;
#else
type.IsGenericType;
#endif
public static bool IsInterface(this Type type) =>
#if NETSTANDARD1_3 || NETCOREAPP1_0
type.GetTypeInfo().IsInterface;
#else
type.IsInterface;
#endif
#if NETSTANDARD1_3 || NETCOREAPP1_0
public static IEnumerable<Attribute> GetCustomAttributes(this Type type, bool inherit)
{
return type.GetTypeInfo().GetCustomAttributes(inherit);
}
public static TypeCode GetTypeCode(Type type)
{
if (type == null) return TypeCode.Empty;
if (typeCodeLookup.TryGetValue(type, out TypeCode result)) return result;
if (type.IsEnum())
{
type = Enum.GetUnderlyingType(type);
if (typeCodeLookup.TryGetValue(type, out result)) return result;
}
return TypeCode.Object;
}
private static readonly Dictionary<Type, TypeCode> typeCodeLookup = new Dictionary<Type, TypeCode>
{
[typeof(bool)] = TypeCode.Boolean,
[typeof(byte)] = TypeCode.Byte,
[typeof(char)] = TypeCode.Char,
[typeof(DateTime)] = TypeCode.DateTime,
[typeof(decimal)] = TypeCode.Decimal,
[typeof(double)] = TypeCode.Double,
[typeof(short)] = TypeCode.Int16,
[typeof(int)] = TypeCode.Int32,
[typeof(long)] = TypeCode.Int64,
[typeof(object)] = TypeCode.Object,
[typeof(sbyte)] = TypeCode.SByte,
[typeof(float)] = TypeCode.Single,
[typeof(string)] = TypeCode.String,
[typeof(ushort)] = TypeCode.UInt16,
[typeof(uint)] = TypeCode.UInt32,
[typeof(ulong)] = TypeCode.UInt64,
};
#else
public static TypeCode GetTypeCode(Type type) => Type.GetTypeCode(type);
#endif
public static MethodInfo GetPublicInstanceMethod(this Type type, string name, Type[] types)
{
#if NETSTANDARD1_3 || NETCOREAPP1_0
var method = type.GetMethod(name, types);
return (method?.IsPublic == true && !method.IsStatic) ? method : null;
#else
return type.GetMethod(name, BindingFlags.Instance | BindingFlags.Public, null, types, null);
#endif
}
=> type.GetMethod(name, BindingFlags.Instance | BindingFlags.Public, null, types, null);
}
}
......@@ -5,7 +5,6 @@ namespace Dapper
{
public static partial class SqlMapper
{
#if !NETSTANDARD1_3 && !NETSTANDARD2_0
/// <summary>
/// A type handler for data-types that are supported by the underlying provider, but which need
/// a well-known UdtTypeName to be specified
......@@ -36,6 +35,5 @@ void ITypeHandler.SetValue(IDbDataParameter parameter, object value)
if(!(value is DBNull)) StructuredHelper.ConfigureUDT(parameter, udtTypeName);
}
}
#endif
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment