这个类,可以转换为任意类型。
格式大概如下:Object source;int? source = new BaseType<int?>(source);1、允许空字符串、 null 或 DBNull。
2、可以转换为可空类型、枚举、基本数据类型。个人能力有限,如下:
public static implicit operator T(BasicType<T> type)
{
Type destinationType = typeof(T); Type sourceType = type._source.GetType(); if (type._source == null || type._source == DBNull.Value) return default(T);
else if (typeof(T).BaseType == typeof(Enum)) return (T)Enum.Parse(typeof(T), type._source.ToStringOrEmpty());
else
{
if (destinationType.IsGenericType)
{
destinationType = destinationType.GetGenericArguments()[0];
type._source = Convert.ChangeType(type._source, destinationType);
} try
{
return (T)type._source;
}
catch (Exception) { }
}
}
格式大概如下:Object source;int? source = new BaseType<int?>(source);1、允许空字符串、 null 或 DBNull。
2、可以转换为可空类型、枚举、基本数据类型。个人能力有限,如下:
public static implicit operator T(BasicType<T> type)
{
Type destinationType = typeof(T); Type sourceType = type._source.GetType(); if (type._source == null || type._source == DBNull.Value) return default(T);
else if (typeof(T).BaseType == typeof(Enum)) return (T)Enum.Parse(typeof(T), type._source.ToStringOrEmpty());
else
{
if (destinationType.IsGenericType)
{
destinationType = destinationType.GetGenericArguments()[0];
type._source = Convert.ChangeType(type._source, destinationType);
} try
{
return (T)type._source;
}
catch (Exception) { }
}
}
在同一个作用范围内,source 是不允许在两处定义的。
switch (column.ColumnName.ToLower())
{
case "doccode":
this._doccode = new BasicType<string>(value);
break;
case "docdate":
this._docdate = new BasicType<DateTime?>(value);
break;
case "companyid":
this._companyid = new BasicType<string>(value);
break;
case "companyname":
this._companyname = new BasicType<string>(value);
break;
case "cltcode":
this._cltcode = new BasicType<string>(value);
break;
case "cltname":
this._cltname = new BasicType<string>(value);
break;
case "stcode":
this._stcode = new BasicType<string>(value);
break;
case "stname":
this._stname = new BasicType<string>(value);
break;
case "tncode":
this._tncode = new BasicType<string>(value);
break;
case "tnname":
this._tnname = new BasicType<string>(value);
break;
case "cashercode":
this._cashercode = new BasicType<string>(value);
break;
case "cashername":
this._cashername = new BasicType<string>(value);
break;
case "address":
this._address = new BasicType<string>(value);
break;
case "tel":
this._tel = new BasicType<string>(value);
break;
case "paymoney":
this._payMoney = new BasicType<decimal?>(value);
break;
case "payticket":
this._payTicket = new BasicType<decimal?>(value);
break;
case "paycard":
this._payCard = new BasicType<decimal?>(value);
break;
case "payzero":
this._payZero = new BasicType<decimal?>(value);
break;
case "payvip":
this._payVIP = new BasicType<decimal?>(value);
break;
case "mustpay":
this._mustPay = new BasicType<decimal?>(value);
break;
case "mustcost":
this._mustCost = new BasicType<decimal?>(value);
break;
case "vipno":
this._vipno = new BasicType<string>(value);
break;
case "vipdiscount":
this._vipdiscount = new BasicType<decimal?>(value);
break;
case "sumdigit":
this._sumdigit = new BasicType<decimal?>(value);
break; default:
throw new Exception("未处理的列:" + column.ColumnName);// 建议保留该处理,可以防止没有处理的列名。
}
你不觉得你那样使用switch,不但没有简化操作,反而看起来更加累啊,至于效率是低得没话说了。
switch 我已经使用一个工具自动生成。但是,我觉得,效率并没有低吧?我没有测试过。
不过,让你一提醒我猛然觉悟,我怎么这么傻……都自动生成了,根本不需要循环和switch。