diff --git a/Dapper NET40/SqlMapper.cs b/Dapper NET40/SqlMapper.cs
index 55fbf87f1167c86b5daa55600428545c5b6dfcbf..1a2e8a2453e13d95600989fa5796b3321bd55ada 100644
--- a/Dapper NET40/SqlMapper.cs
+++ b/Dapper NET40/SqlMapper.cs
@@ -8,6 +8,15 @@
#if DNXCORE50
using IDbDataParameter = System.Data.Common.DbParameter;
+using IDataParameter = System.Data.Common.DbParameter;
+using IDbTransaction = System.Data.Common.DbTransaction;
+using IDbConnection = System.Data.Common.DbConnection;
+using IDbCommand = System.Data.Common.DbCommand;
+using IDataReader = System.Data.Common.DbDataReader;
+using IDataRecord = System.Data.Common.DbDataReader;
+using IDataParameterCollection = System.Data.Common.DbParameterCollection;
+using DataException = System.InvalidOperationException;
+using ApplicationException = System.InvalidOperationException;
#endif
using System;
@@ -24,6 +33,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq.Expressions;
+using System.Data.Common;
namespace Dapper
{
@@ -312,7 +322,7 @@ public interface ITypeHandler
/// The typed value
object Parse(Type destinationType, object value);
}
-
+#if !DNXCORE50
///
/// A type handler for data-types that are supported by the underlying provider, but which need
/// a well-known UdtTypeName to be specified
@@ -342,6 +352,7 @@ void ITypeHandler.SetValue(IDbDataParameter parameter, object value)
}
}
}
+#endif
///
/// Base-class for simple type-handlers
@@ -726,8 +737,9 @@ static SqlMapper()
typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset;
typeMap[typeof(TimeSpan?)] = DbType.Time;
typeMap[typeof(object)] = DbType.Object;
-
+#if !DNXCORE50
AddTypeHandlerImpl(typeof(DataTable), new DataTableHandler(), false);
+#endif
}
///
@@ -736,7 +748,9 @@ static SqlMapper()
public static void ResetTypeHandlers()
{
typeHandlers = new Dictionary();
+#if !DNXCORE50
AddTypeHandlerImpl(typeof(DataTable), new DataTableHandler(), true);
+#endif
}
///
/// Configure the specified type to be mapped to a given db-type
@@ -770,7 +784,7 @@ public static void AddTypeHandlerImpl(Type type, ITypeHandler handler, bool clon
if (type == null) throw new ArgumentNullException("type");
Type secondary = null;
- if(type.IsValueType)
+ if(type.IsValueType())
{
var underlying = Nullable.GetUnderlyingType(type);
if(underlying == null)
@@ -823,7 +837,10 @@ public static void AddTypeHandler(TypeHandler handler)
/// Not intended for direct usage
///
[Obsolete("Not intended for direct usage", false)]
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
+#if !DNXCORE50
+ [Browsable(false)]
+#endif
+ [EditorBrowsable(EditorBrowsableState.Never)]
public static class TypeHandlerCache
{
///
@@ -862,7 +879,11 @@ internal static void SetHandler(ITypeHandler handler)
///
/// Get the DbType that maps to a given value
///
- [Obsolete("This method is for internal use only"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
+ [Obsolete("This method is for internal use only")]
+#if !DNXCORE50
+ [Browsable(false)]
+#endif
+ [EditorBrowsable(EditorBrowsableState.Never)]
public static DbType GetDbType(object value)
{
if (value == null || value is DBNull) return DbType.Object;
@@ -877,7 +898,7 @@ internal static DbType LookupDbType(Type type, string name, bool demand, out ITy
handler = null;
var nullUnderlyingType = Nullable.GetUnderlyingType(type);
if (nullUnderlyingType != null) type = nullUnderlyingType;
- if (type.IsEnum && !typeMap.ContainsKey(type))
+ if (type.IsEnum() && !typeMap.ContainsKey(type))
{
type = Enum.GetUnderlyingType(type);
}
@@ -898,6 +919,7 @@ internal static DbType LookupDbType(Type type, string name, bool demand, out ITy
{
return DbType.Object;
}
+#if !DNXCORE50
switch (type.FullName)
{
case "Microsoft.SqlServer.Types.SqlGeography":
@@ -910,6 +932,7 @@ internal static DbType LookupDbType(Type type, string name, bool demand, out ITy
AddTypeHandler(type, handler = new UdtTypeHandler("hierarchyid"));
return DbType.Object;
}
+#endif
if(demand)
throw new NotSupportedException(string.Format("The member {0} of type {1} cannot be used as a parameter value", name, type.FullName));
return DbType.Object;
@@ -2140,13 +2163,13 @@ private static void PassByPosition(IDbCommand cmd)
{
if (cmd.Parameters.Count == 0) return;
- Dictionary parameters = new Dictionary(StringComparer.InvariantCulture);
+ Dictionary parameters = new Dictionary(StringComparer.Ordinal);
foreach(IDbDataParameter param in cmd.Parameters)
{
if (!string.IsNullOrEmpty(param.ParameterName)) parameters[param.ParameterName] = param;
}
- HashSet consumed = new HashSet(StringComparer.InvariantCulture);
+ HashSet consumed = new HashSet(StringComparer.Ordinal);
bool firstMatch = true;
cmd.CommandText = pseudoPositional.Replace(cmd.CommandText, match =>
{
@@ -2193,8 +2216,8 @@ private static void PassByPosition(IDbCommand cmd)
}
#endif
Type underlyingType = null;
- if (!(typeMap.ContainsKey(type) || type.IsEnum || type.FullName == LinqBinary ||
- (type.IsValueType && (underlyingType = Nullable.GetUnderlyingType(type)) != null && underlyingType.IsEnum)))
+ if (!(typeMap.ContainsKey(type) || type.IsEnum() || type.FullName == LinqBinary ||
+ (type.IsValueType() && (underlyingType = Nullable.GetUnderlyingType(type)) != null && underlyingType.IsEnum())))
{
ITypeHandler handler;
if (typeHandlers.TryGetValue(type, out handler))
@@ -2652,7 +2675,10 @@ private static Exception MultiMapException(IDataRecord reader)
///
///
///
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
+#if !DNXCORE50
+ [Browsable(false)]
+#endif
+ [EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is for internal usage only", false)]
public static char ReadChar(object value)
{
@@ -2665,7 +2691,10 @@ public static char ReadChar(object value)
///
/// Internal use only
///
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
+#if !DNXCORE50
+ [Browsable(false)]
+#endif
+ [EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is for internal usage only", false)]
public static char? ReadNullableChar(object value)
{
@@ -2679,7 +2708,10 @@ public static char ReadChar(object value)
///
/// Internal use only
///
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
+#if !DNXCORE50
+ [Browsable(false)]
+#endif
+ [EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is for internal usage only", true)]
public static IDbDataParameter FindOrAddParameter(IDataParameterCollection parameters, IDbCommand command, string name)
{
@@ -2700,7 +2732,10 @@ public static IDbDataParameter FindOrAddParameter(IDataParameterCollection param
///
/// Internal use only
///
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
+#if !DNXCORE50
+ [Browsable(false)]
+#endif
+ [EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is for internal usage only", false)]
public static void PackListParameters(IDbCommand command, string namePrefix, object value)
{
@@ -2854,10 +2889,12 @@ public static string Format(object value)
}
else
{
- switch (Type.GetTypeCode(value.GetType()))
+ switch (TypeExtensions.GetTypeCode(value.GetType()))
{
+#if !DNXCORE50
case TypeCode.DBNull:
return "null";
+#endif
case TypeCode.Boolean:
return ((bool)value) ? "1" : "0";
case TypeCode.Byte:
@@ -2936,7 +2973,7 @@ internal static IList GetLiteralTokens(string sql)
if (!literalTokens.IsMatch(sql)) return LiteralToken.None;
var matches = literalTokens.Matches(sql);
- var found = new HashSet(StringComparer.InvariantCulture);
+ var found = new HashSet(StringComparer.Ordinal);
List list = new List(matches.Count);
foreach(Match match in matches)
{
@@ -2970,7 +3007,7 @@ internal static IList GetLiteralTokens(string sql)
var il = dm.GetILGenerator();
- bool isStruct = type.IsValueType;
+ bool isStruct = type.IsValueType();
bool haveInt32Arg1 = false;
il.Emit(OpCodes.Ldarg_1); // stack is now [untyped-param]
if (isStruct)
@@ -3000,7 +3037,7 @@ internal static IList GetLiteralTokens(string sql)
bool ok = true;
for (int i = 0; i < propsArr.Length; i++)
{
- if (!string.Equals(propsArr[i].Name, ctorParams[i].Name, StringComparison.InvariantCultureIgnoreCase))
+ if (!string.Equals(propsArr[i].Name, ctorParams[i].Name, StringComparison.OrdinalIgnoreCase))
{
ok = false;
break;
@@ -3012,7 +3049,7 @@ internal static IList GetLiteralTokens(string sql)
props = propsArr;
}
else { // might still all be accounted for; check the hard way
- var positionByName = new Dictionary(StringComparer.InvariantCultureIgnoreCase);
+ var positionByName = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach(var param in ctorParams)
{
positionByName[param.Name] = param.Position;
@@ -3066,7 +3103,7 @@ internal static IList GetLiteralTokens(string sql)
il.Emit(OpCodes.Ldstr, prop.Name); // stack is now [parameters] [command] [name]
il.Emit(OpCodes.Ldloc_0); // stack is now [parameters] [command] [name] [typed-param]
il.Emit(callOpCode, prop.GetGetMethod()); // stack is [parameters] [command] [name] [typed-value]
- if (prop.PropertyType.IsValueType)
+ if (prop.PropertyType.IsValueType())
{
il.Emit(OpCodes.Box, prop.PropertyType); // stack is [parameters] [command] [name] [boxed-value]
}
@@ -3118,7 +3155,7 @@ internal static IList GetLiteralTokens(string sql)
il.Emit(OpCodes.Ldloc_0); // stack is now [parameters] [[parameters]] [parameter] [parameter] [typed-param]
il.Emit(callOpCode, prop.GetGetMethod()); // stack is [parameters] [[parameters]] [parameter] [parameter] [typed-value]
bool checkForNull = true;
- if (prop.PropertyType.IsValueType)
+ if (prop.PropertyType.IsValueType())
{
il.Emit(OpCodes.Box, prop.PropertyType); // stack is [parameters] [[parameters]] [parameter] [parameter] [boxed-value]
if (Nullable.GetUnderlyingType(prop.PropertyType) == null)
@@ -3228,10 +3265,10 @@ internal static IList GetLiteralTokens(string sql)
for(int i = 0; i < propsArr.Length;i++)
{
string thisName = propsArr[i].Name;
- if(string.Equals(thisName, huntName, StringComparison.InvariantCultureIgnoreCase))
+ if(string.Equals(thisName, huntName, StringComparison.OrdinalIgnoreCase))
{
fallback = propsArr[i];
- if(string.Equals(thisName, huntName, StringComparison.InvariantCulture))
+ if(string.Equals(thisName, huntName, StringComparison.Ordinal))
{
exact = fallback;
break;
@@ -3246,7 +3283,7 @@ internal static IList GetLiteralTokens(string sql)
il.Emit(OpCodes.Ldloc_0); // command, sql, typed parameter
il.EmitCall(callOpCode, prop.GetGetMethod(), null); // command, sql, typed value
Type propType = prop.PropertyType;
- var typeCode = Type.GetTypeCode(propType);
+ var typeCode = TypeExtensions.GetTypeCode(propType);
switch (typeCode)
{
case TypeCode.Boolean:
@@ -3287,7 +3324,7 @@ internal static IList GetLiteralTokens(string sql)
il.EmitCall(OpCodes.Call, convert, null); // command, sql, string value
break;
default:
- if (propType.IsValueType) il.Emit(OpCodes.Box, propType); // command, sql, object value
+ if (propType.IsValueType()) il.Emit(OpCodes.Box, propType); // command, sql, object value
il.EmitCall(OpCodes.Call, format, null); // command, sql, string value
break;
@@ -3305,13 +3342,13 @@ internal static IList GetLiteralTokens(string sql)
{
typeof(bool), typeof(sbyte), typeof(byte), typeof(ushort), typeof(short),
typeof(uint), typeof(int), typeof(ulong), typeof(long), typeof(float), typeof(double), typeof(decimal)
- }.ToDictionary(x => Type.GetTypeCode(x), x => x.GetMethod("ToString", BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(IFormatProvider) }, null));
+ }.ToDictionary(x => TypeExtensions.GetTypeCode(x), x => x.GetPublicInstanceMethod("ToString", new[] { typeof(IFormatProvider) }));
static MethodInfo GetToString(TypeCode typeCode)
{
MethodInfo method;
return toStrings.TryGetValue(typeCode, out method) ? method : null;
}
- static readonly MethodInfo StringReplace = typeof(string).GetMethod("Replace", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(string), typeof(string) }, null),
+ static readonly MethodInfo StringReplace = typeof(string).GetPublicInstanceMethod("Replace", new Type[] { typeof(string), typeof(string) }),
InvariantCulture = typeof(CultureInfo).GetProperty("InvariantCulture", BindingFlags.Public | BindingFlags.Static).GetGetMethod();
private static int ExecuteCommand(IDbConnection cnn, ref CommandDefinition command, Action paramReader)
@@ -3423,7 +3460,7 @@ private static IDataReader ExecuteReaderImpl(IDbConnection cnn, ref CommandDefin
}
#pragma warning restore 618
- if (effectiveType.IsEnum)
+ if (effectiveType.IsEnum())
{ // assume the value is returned as the correct type (int/byte/etc), but box back to the typed enum
return r =>
{
@@ -3457,7 +3494,7 @@ private static T Parse(object value)
if (value is T) return (T)value;
var type = typeof(T);
type = Nullable.GetUnderlyingType(type) ?? type;
- if (type.IsEnum)
+ if (type.IsEnum())
{
if (value is float || value is double || value is decimal)
{
@@ -3486,14 +3523,23 @@ static readonly MethodInfo
public static ITypeMap GetTypeMap(Type type)
{
if (type == null) throw new ArgumentNullException("type");
+#if DNXCORE50
+ ITypeMap map = null;
+#else
var map = (ITypeMap)_typeMaps[type];
+#endif
if (map == null)
{
lock (_typeMaps)
{ // double-checked; store this to avoid reflection next time we see this type
// since multiple queries commonly use the same domain-entity/DTO/view-model type
+#if DNXCORE50
+ if (!_typeMaps.TryGetValue(type, out map)) map = null;
+#else
map = (ITypeMap)_typeMaps[type];
- if (map == null)
+#endif
+
+ if (map == null)
{
map = new DefaultTypeMap(type);
_typeMaps[type] = map;
@@ -3504,7 +3550,11 @@ public static ITypeMap GetTypeMap(Type type)
}
// use Hashtable to get free lockless reading
+#if DNXCORE50
+ private static readonly Dictionary _typeMaps = new Dictionary();
+#else
private static readonly Hashtable _typeMaps = new Hashtable();
+#endif
///
/// Set custom mapping for type deserializers
@@ -3577,8 +3627,10 @@ public static void SetTypeMap(Type type, ITypeMap map)
ConstructorInfo specializedConstructor = null;
+#if !DNXCORE50
bool supportInitialize = false;
- if (type.IsValueType)
+#endif
+ if (type.IsValueType())
{
il.Emit(OpCodes.Ldloca_S, (byte)1);
il.Emit(OpCodes.Initobj, type);
@@ -3599,7 +3651,7 @@ public static void SetTypeMap(Type type, ITypeMap map)
var consPs = explicitConstr.GetParameters();
foreach(var p in consPs)
{
- if(!p.ParameterType.IsValueType)
+ if(!p.ParameterType.IsValueType())
{
il.Emit(OpCodes.Ldnull);
}
@@ -3620,12 +3672,14 @@ public static void SetTypeMap(Type type, ITypeMap map)
il.Emit(OpCodes.Newobj, explicitConstr);
il.Emit(OpCodes.Stloc_1);
+#if !DNXCORE50
supportInitialize = typeof(ISupportInitialize).IsAssignableFrom(type);
if (supportInitialize)
{
il.Emit(OpCodes.Ldloc_1);
il.EmitCall(OpCodes.Callvirt, typeof(ISupportInitialize).GetMethod("BeginInit"), null);
}
+#endif
}
else
{
@@ -3640,12 +3694,14 @@ public static void SetTypeMap(Type type, ITypeMap map)
{
il.Emit(OpCodes.Newobj, ctor);
il.Emit(OpCodes.Stloc_1);
+#if !DNXCORE50
supportInitialize = typeof(ISupportInitialize).IsAssignableFrom(type);
if (supportInitialize)
{
il.Emit(OpCodes.Ldloc_1);
il.EmitCall(OpCodes.Callvirt, typeof(ISupportInitialize).GetMethod("BeginInit"), null);
}
+#endif
}
else
{
@@ -3655,7 +3711,7 @@ public static void SetTypeMap(Type type, ITypeMap map)
}
il.BeginExceptionBlock();
- if (type.IsValueType)
+ if (type.IsValueType())
{
il.Emit(OpCodes.Ldloca_S, (byte)1);// [target]
}
@@ -3706,9 +3762,9 @@ public static void SetTypeMap(Type type, ITypeMap map)
// unbox nullable enums as the primitive, i.e. byte etc
var nullUnderlyingType = Nullable.GetUnderlyingType(memberType);
- var unboxType = nullUnderlyingType != null && nullUnderlyingType.IsEnum ? nullUnderlyingType : memberType;
+ var unboxType = nullUnderlyingType != null && nullUnderlyingType.IsEnum() ? nullUnderlyingType : memberType;
- if (unboxType.IsEnum)
+ if (unboxType.IsEnum())
{
Type numericType = Enum.GetUnderlyingType(unboxType);
if(colType == typeof(string))
@@ -3743,9 +3799,9 @@ public static void SetTypeMap(Type type, ITypeMap map)
}
else
{
- TypeCode dataTypeCode = Type.GetTypeCode(colType), unboxTypeCode = Type.GetTypeCode(unboxType);
+ TypeCode dataTypeCode = TypeExtensions.GetTypeCode(colType), unboxTypeCode = TypeExtensions.GetTypeCode(unboxType);
bool hasTypeHandler;
- if ((hasTypeHandler = typeHandlers.ContainsKey(unboxType)) || colType == unboxType || dataTypeCode == unboxTypeCode || dataTypeCode == Type.GetTypeCode(nullUnderlyingType))
+ if ((hasTypeHandler = typeHandlers.ContainsKey(unboxType)) || colType == unboxType || dataTypeCode == unboxTypeCode || dataTypeCode == TypeExtensions.GetTypeCode(nullUnderlyingType))
{
if (hasTypeHandler)
{
@@ -3776,7 +3832,7 @@ public static void SetTypeMap(Type type, ITypeMap map)
// Store the value in the property/field
if (item.Property != null)
{
- if (type.IsValueType)
+ if (type.IsValueType())
{
il.Emit(OpCodes.Call, DefaultTypeMap.GetPropertySetter(item.Property, type)); // stack is now [target]
}
@@ -3797,7 +3853,7 @@ public static void SetTypeMap(Type type, ITypeMap map)
if (specializedConstructor != null)
{
il.Emit(OpCodes.Pop);
- if (item.MemberType.IsValueType)
+ if (item.MemberType.IsValueType())
{
int localIndex = il.DeclareLocal(item.MemberType).LocalIndex;
LoadLocalAddress(il, localIndex);
@@ -3828,7 +3884,7 @@ public static void SetTypeMap(Type type, ITypeMap map)
first = false;
index += 1;
}
- if (type.IsValueType)
+ if (type.IsValueType())
{
il.Emit(OpCodes.Pop);
}
@@ -3839,11 +3895,13 @@ public static void SetTypeMap(Type type, ITypeMap map)
il.Emit(OpCodes.Newobj, specializedConstructor);
}
il.Emit(OpCodes.Stloc_1); // stack is empty
+#if !DNXCORE50
if (supportInitialize)
{
il.Emit(OpCodes.Ldloc_1);
il.EmitCall(OpCodes.Callvirt, typeof(ISupportInitialize).GetMethod("EndInit"), null);
}
+#endif
}
il.MarkLabel(allDone);
il.BeginCatchBlock(typeof(Exception)); // stack is Exception
@@ -3854,7 +3912,7 @@ public static void SetTypeMap(Type type, ITypeMap map)
il.EndExceptionBlock();
il.Emit(OpCodes.Ldloc_1); // stack is [rval]
- if (type.IsValueType)
+ if (type.IsValueType())
{
il.Emit(OpCodes.Box, type);
}
@@ -3880,7 +3938,7 @@ private static void FlexibleConvertBoxedFromHeadOfStack(ILGenerator il, Type fro
{
bool handled = false;
OpCode opCode = default(OpCode);
- switch (Type.GetTypeCode(from))
+ switch (TypeExtensions.GetTypeCode(from))
{
case TypeCode.Boolean:
case TypeCode.Byte:
@@ -3894,7 +3952,7 @@ private static void FlexibleConvertBoxedFromHeadOfStack(ILGenerator il, Type fro
case TypeCode.Single:
case TypeCode.Double:
handled = true;
- switch (Type.GetTypeCode(via ?? to))
+ switch (TypeExtensions.GetTypeCode(via ?? to))
{
case TypeCode.Byte:
opCode = OpCodes.Conv_Ovf_I1_Un; break;
@@ -4043,7 +4101,7 @@ public static void ThrowDataException(Exception ex, int index, IDataReader reade
}
else
{
- formattedValue = Convert.ToString(value) + " - " + Type.GetTypeCode(value.GetType());
+ formattedValue = Convert.ToString(value) + " - " + TypeExtensions.GetTypeCode(value.GetType());
}
}
catch (Exception valEx)
@@ -4379,6 +4437,8 @@ public void Dispose()
}
}
+
+#if !DNXCORE50
///
/// Used to pass a DataTable as a TableValuedParameter
///
@@ -4412,7 +4472,7 @@ public static string GetTypeName(this DataTable table)
{
return table == null ? null : table.ExtendedProperties[DataTableTypeNameKey] as string;
}
-
+#endif
// one per thread
[ThreadStatic]
@@ -4793,8 +4853,8 @@ public DynamicParameters Output(T target, Expression> express
var lastMemberAccess = expression.Body as MemberExpression;
if (lastMemberAccess == null ||
- (lastMemberAccess.Member.MemberType != MemberTypes.Property &&
- lastMemberAccess.Member.MemberType != MemberTypes.Field))
+ (!(lastMemberAccess.Member is PropertyInfo) &&
+ !(lastMemberAccess.Member is FieldInfo)))
{
if (expression.Body.NodeType == ExpressionType.Convert &&
expression.Body.Type == typeof(object) &&
@@ -4829,8 +4889,8 @@ public DynamicParameters Output(T target, Expression> express
break;
}
else if (diving == null ||
- (diving.Member.MemberType != MemberTypes.Property &&
- diving.Member.MemberType != MemberTypes.Field))
+ (!(diving.Member is PropertyInfo) &&
+ !(diving.Member is FieldInfo)))
{
@throw();
}
@@ -4843,8 +4903,15 @@ public DynamicParameters Output(T target, Expression> express
var lookup = string.Join("|", names.ToArray());
var cache = CachedOutputSetters.Cache;
- var setter = (Action