/// <summary> /// 动态获取IDataRecord里面的函数 /// </summary> /// <param name="generator"></param> /// <param name="type"></param> /// <param name="isNullable"></param> /// <param name="pro"></param> /// <param name="dbTypeName"></param> /// <param name="fieldName"></param> private static void GeneratorCallMethod(ILGenerator generator, Type type, bool isNullable, PropertyInfo pro, string dbTypeName, string fieldName) { List <string> guidThrow = new List <string>() { "int32", "datetime", "decimal", "double", "byte", "string" }; //数据库为GUID有错的实体类形 List <string> intThrow = new List <string>() { "datetime", "byte" }; //数据库为int有错的实体类形 List <string> stringThrow = new List <string>() { "int32", "datetime", "decimal", "double", "byte", "guid" }; //数据库为vachar有错的实体类形 List <string> decimalThrow = new List <string>() { "datetime", "byte", "guid" }; List <string> doubleThrow = new List <string>() { "datetime", "byte", "guid" }; List <string> dateThrow = new List <string>() { "int32", "decimal", "double", "byte", "guid" }; List <string> shortThrow = new List <string>() { "datetime", "guid" }; List <string> byteThrow = new List <string>() { "datetime", "guid" }; MethodInfo method = null; var typeName = SqlSugarTool.ChangeDBTypeToCSharpType(dbTypeName); var objTypeName = type.Name.ToLower(); var isEnum = type.IsEnum; if (isEnum) { typeName = "ENUMNAME"; } else if (dbTypeName.Contains("hierarchyid") || typeName == "byte[]" || objTypeName == "object") { generator.Emit(OpCodes.Call, getValueMethod); generator.Emit(OpCodes.Unbox_Any, pro.PropertyType);//找不到类型才执行拆箱(类型转换) return; } if (isNullable) { switch (typeName) { case "int": CheckType(intThrow, objTypeName, typeName, fieldName); var isNotInt = objTypeName != "int32"; if (isNotInt) { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertInt32; } break; case "long": CheckType(intThrow, objTypeName, typeName, fieldName); var isNotLong = objTypeName != "int64"; if (isNotLong) { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvetInt64; } break; case "bool": if (objTypeName != "bool" && objTypeName != "boolean") { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertBoolean; } break; case "string": CheckType(stringThrow, objTypeName, typeName, fieldName); method = getString; break; case "dateTime": CheckType(dateThrow, objTypeName, typeName, fieldName); if (objTypeName != "datetime") { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertDateTime; } break; case "decimal": CheckType(decimalThrow, objTypeName, typeName, fieldName); var isNotDecimal = objTypeName != "decimal"; if (isNotDecimal) { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertDecimal; } break; case "double": CheckType(doubleThrow, objTypeName, typeName, fieldName); var isNotDouble = objTypeName != "double"; if (isNotDouble) { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertDouble; } break; case "float": CheckType(decimalThrow, objTypeName, typeName, fieldName); if (objTypeName != "float" && objTypeName != "single") { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertFloat; } break; case "guid": CheckType(guidThrow, objTypeName, typeName, fieldName); if (objTypeName != "guid") { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertGuid; } break; case "byte": CheckType(byteThrow, objTypeName, typeName, fieldName); if (objTypeName != "byte") { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertByte; } break; case "ENUMNAME": method = getConvertToEnum_Nullable.MakeGenericMethod(type); break; case "short": CheckType(shortThrow, objTypeName, typeName, fieldName); var isNotShort = objTypeName != "int16" && objTypeName != "short"; if (isNotShort) { method = getOtherNull.MakeGenericMethod(type); } else { method = getConvertInt16; } break; default: method = getOtherNull.MakeGenericMethod(type); break; } generator.Emit(OpCodes.Call, method); } else { switch (typeName) { case "int": CheckType(intThrow, objTypeName, typeName, fieldName); var isNotInt = objTypeName != "int32"; if (isNotInt) { method = getOther.MakeGenericMethod(type); } else { method = getInt32; } break; case "long": CheckType(intThrow, objTypeName, typeName, fieldName); var isNotLong = objTypeName != "int64"; if (isNotLong) { method = getOther.MakeGenericMethod(type); } else { method = getInt64; } break; case "bool": if (objTypeName != "bool" && objTypeName != "boolean") { method = getOther.MakeGenericMethod(type); } else { method = getBoolean; } break; case "string": CheckType(stringThrow, objTypeName, typeName, fieldName); method = getString; break; case "dateTime": CheckType(dateThrow, objTypeName, typeName, fieldName); if (objTypeName != "datetime") { method = getOther.MakeGenericMethod(type); } else { method = getDateTime; } break; case "decimal": CheckType(decimalThrow, objTypeName, typeName, fieldName); var isNotDecimal = objTypeName != "decimal"; if (isNotDecimal) { method = getOther.MakeGenericMethod(type); } else { method = getDecimal; } break; case "double": CheckType(doubleThrow, objTypeName, typeName, fieldName); var isNotDouble = objTypeName != "double"; if (isNotDouble) { method = getOther.MakeGenericMethod(type); } else { method = getDouble; } break; case "float": CheckType(decimalThrow, objTypeName, typeName, fieldName); if (objTypeName != "float" && objTypeName != "single") { method = getOther.MakeGenericMethod(type); } else { method = getFloat; } break; case "guid": CheckType(guidThrow, objTypeName, typeName, fieldName); if (objTypeName != "guid") { method = getOther.MakeGenericMethod(type); } else { method = getGuid; } break; case "byte": CheckType(byteThrow, objTypeName, typeName, fieldName); if (objTypeName != "byte") { method = getOther.MakeGenericMethod(type); } else { method = getByte; } break; case "ENUMNAME": method = getValueMethod; break; case "short": CheckType(shortThrow, objTypeName, typeName, fieldName); var isNotShort = objTypeName != "int16" && objTypeName != "short"; if (isNotShort) { method = getOther.MakeGenericMethod(type); } else { method = getInt16; } break; default: method = getOther.MakeGenericMethod(type); break;; } generator.Emit(OpCodes.Call, method); if (method == getValueMethod) { generator.Emit(OpCodes.Unbox_Any, pro.PropertyType);//找不到类型才执行拆箱(类型转换) } } }