public static MSDBAttribute[] ObjectToAttrs(object tar) { Type obj = tar.GetType(); var fields = obj.GetFields(); MSDBAttribute[] ms = new MSDBAttribute[fields.Length]; for (int i = 0; i < fields.Length; i++) { var f = fields[i]; var atts = f.GetCustomAttributes(msdbType, false); if (atts.Length > 0) { ms[i] = atts[0] as MSDBAttribute; ms[i].FieldName = f.Name; if (ms[i].DbType >= MySqlDataType.CHAR) { ms[i].Value = f.GetValue(tar) as string; } else { ms[i].Value = f.GetValue(tar).ToString(); } } } return(ms); }
public static MSDBAttribute[] TypeToAttrs(Type obj) { var fields = obj.GetFields(); MSDBAttribute[] ms = new MSDBAttribute[fields.Length]; for (int i = 0; i < fields.Length; i++) { var f = fields[i]; var atts = f.GetCustomAttributes(msdbType, false); if (atts.Length > 0) { ms[i] = atts[0] as MSDBAttribute; ms[i].FieldName = f.Name.ToLower(); } } return(ms); }
static void CheckValue(MSDBAttribute ms) { if (ms.Value == null) { return; } if (ms.DbType < MySqlDataType.CHAR) { if (ms.DbType < MySqlDataType.FLOAT)//整数型 { var v = ms.Value; for (int i = 0; i < v.Length; i++) { if (Nums.IndexOf(v[i]) < 0) { if (i == 0) { ms.Value = null; } else { ms.Value = v.Substring(0, i); } return; } } } else if (ms.DbType < MySqlDataType.DATE)//小数型 { var v = ms.Value; int dc = 0; for (int i = 0; i < v.Length; i++) { if (v[i] == '.') { dc++; if (dc > 1) { if (i == 1) { ms.Value = null; } else { v = v.Substring(0, i); if (v[0] == '.') { v = "0" + v; } ms.Value = v; } return; } } else if (Nums.IndexOf(v[i]) < 0) { if (i == 0) { ms.Value = null; } else { v = v.Substring(0, i); if (v[0] == '.') { v = "0" + v; } ms.Value = v; } return; } } } } }