Skip to content

Commit

Permalink
优化。
Browse files Browse the repository at this point in the history
  • Loading branch information
cyq1162 committed May 19, 2019
1 parent ada58b0 commit 50363f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 91 deletions.
116 changes: 29 additions & 87 deletions Table/MDataCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ private void CheckNewValue()
private void NullCheck()
{
bool valueIsNull = CellValue.SourceValue == null || CellValue.SourceValue == DBNull.Value;
if (!valueIsNull && CellValue.StringValue.Length == 0 && DataType.GetGroup(_CellStruct.SqlType) > 0)
{
valueIsNull = true;
}
if (valueIsNull)
{
if (CellValue.IsNull)
Expand Down Expand Up @@ -217,113 +221,51 @@ internal void FixValue()
isAllowChangeState = false;//恢复可设置状态。
}

internal object ChangeValue(object value, Type convertionType, int groupID)
{
Exception err;
return ChangeValue(value, convertionType, groupID, out err);
}
/// <summary>
/// 值的数据类型转换。
/// </summary>
/// <param name="value">要被转换的值</param>
/// <param name="convertionType">要转换成哪种类型</param>
/// <param name="groupID">数据库类型的组号</param>
/// <returns></returns>
internal object ChangeValue(object value, Type convertionType, int groupID, out Exception ex)
internal object ChangeValue(object value, Type convertionType, int groupID)
{
ex = null;
if (value == null)
{
CellValue.IsNull = true;
return value;
}
if (groupID > 0 && StringValue == "")
{
CellValue.IsNull = true;
return null;
}
//值不为null
try
{
#region 类型转换
if (groupID == 1)
switch (groupID)
{
switch (StringValue)
{
case "正无穷大":
CellValue.StringValue = "Infinity";
break;
case "负无穷大":
CellValue.StringValue = "-Infinity";
break;
}
}
Type valueType = value.GetType();
if (valueType.IsEnum && groupID == 1)
{
if (convertionType.Name.StartsWith("Int"))
{
return (int)value;
}
return Convert.ChangeType((int)value, convertionType);
}
if (valueType != convertionType)
{
#region 折叠
switch (groupID)
{
case 0:
if (_CellStruct.SqlType == SqlDbType.Time)//time类型的特殊处理。
{
string[] items = StringValue.Split(' ');
if (items.Length > 1)
{
CellValue.StringValue = items[1];
}
}
value = StringValue;
break;
case 1:
switch (StringValue.ToLower())
{
case "true":
value = 1;
break;
case "false":
value = 0;
break;
case "infinity":
value = double.PositiveInfinity;
break;
case "-infinity":
value = double.NegativeInfinity;
break;
default:
goto err;
}
break;
default:
err:
if (convertionType.Name.EndsWith("[]"))
{
value = Convert.FromBase64String(StringValue);
CellValue.StringValue = "System.Byte[]";
}
else
case 0:
if (_CellStruct.SqlType == SqlDbType.Time)//time类型的特殊处理。
{
string[] items = StringValue.Split(' ');
if (items.Length > 1)
{
value = ConvertTool.ChangeType(value, convertionType);
CellValue.StringValue = items[1];
}
break;
}
#endregion
}
value = StringValue;
break;

default:
value = ConvertTool.ChangeType(value, convertionType);
//if (convertionType.Name.EndsWith("[]"))
//{
// value = Convert.FromBase64String(StringValue);
// CellValue.StringValue = "System.Byte[]";
//}
//else
//{

//}
break;
}
#endregion
}
catch (Exception err)
{
CellValue.Value = null;
CellValue.IsNull = true;
CellValue.StringValue = null;
ex = err;
string msg = string.Format("ChangeType Error:ColumnName【{0}】({1}) , Value:【{2}】\r\n", _CellStruct.ColumnName, _CellStruct.ValueType.FullName, StringValue);

Log.Write(msg, LogType.Error);
Expand Down
28 changes: 24 additions & 4 deletions Tool/ConvertTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public static object ChangeType(object value, Type t)
case "current_timestamp":
return DateTime.Now;
}
if (DateTime.Parse(strValue) == DateTime.MinValue) {
if (DateTime.Parse(strValue) == DateTime.MinValue)
{
return (DateTime)SqlDateTime.MinValue;
}
return Convert.ChangeType(value, t);//这里用value,避免丢失毫秒
Expand All @@ -75,12 +76,19 @@ public static object ChangeType(object value, Type t)
{
if (strValue == SqlValue.Guid || strValue.StartsWith("newid"))
{
return Guid.NewGuid();
return Guid.NewGuid();
}
return new Guid(strValue);
}
else if (t.Name.StartsWith("Int"))
{
switch (strValue.ToLower())
{
case "true":
return 1;
case "false":
return 0;
}
if (strValue.IndexOf('.') > -1)
{
strValue = strValue.Split('.')[0];
Expand All @@ -90,6 +98,18 @@ public static object ChangeType(object value, Type t)
return (int)value;
}
}
else if (t.Name == "Double" || t.Name == "Single")
{
switch (strValue.ToLower())
{
case "infinity":
case "正无穷大":
return double.PositiveInfinity;
case "-infinity":
case "负无穷大":
return double.NegativeInfinity;
}
}
else if (t.Name == "Boolean")
{
switch (strValue.ToLower())
Expand Down Expand Up @@ -148,14 +168,14 @@ public static object ChangeType(object value, Type t)
return Convert.ChangeType(value, t);
}
}

/// <summary>
/// 类型转换(精准强大)
/// </summary>
public static T ChangeType<T>(object value)
{
return (T)ChangeType(value, typeof(T));
}

}
}

0 comments on commit 50363f3

Please sign in to comment.