public void DatasetExportToTemplateFile(string templatePath) { var parentKeys = new Stack <ParentKeysInfo>(); foreach (DataRow fileRow in mDataset.Tables["@_root"].Rows) { object obj = Form1.Inst.m_ServerCommonAssembly.CreateInstance(mTableNameToTypeName["@_root"]); //mCurFileName = fileRow["@filename"].ToString(); ParentKeysInfo key = new ParentKeysInfo(); key.KeyName = "@filename"; key.value = fileRow["@filename"]; key.valueType = typeof(string); key.parentTableName = fileRow.Table.TableName; parentKeys.Push(key); FillObject(obj, fileRow, parentKeys, null); parentKeys.Pop(); ServerFrame.Config.IConfigurator.SaveProperty(obj, obj.GetType().ToString(), templatePath + "\\" + fileRow["@filename"]); } }
private DataSet _CreateDateSetFromFile(string suffix, IEnumerable <string> files, out Type curType) { // var aTypes = Form1.Inst.m_ServerCommonAssembly.GetTypes(); // curType = null; // foreach (var type in aTypes) // { // var atts = type.GetCustomAttributes(typeof(ServerFrame.Editor.CDataEditorAttribute), true); // // if (atts.Length > 0) // { // ServerFrame.Editor.CDataEditorAttribute dea = atts[0] as ServerFrame.Editor.CDataEditorAttribute; // if (dea.m_strFileExt == suffix) // { // curType = type; // break; // } // } // } curType = null; var aTypes = Form1.Inst.m_CSCommonAssembly.GetTypes(); foreach (var type in aTypes) { var atts = type.GetCustomAttributes(typeof(ServerFrame.Editor.CDataEditorAttribute), true); if (atts.Length > 0) { ServerFrame.Editor.CDataEditorAttribute dea = atts[0] as ServerFrame.Editor.CDataEditorAttribute; if (dea.m_strFileExt == suffix) { curType = type; break; } } } mDataset = new DataSet(); mExportFileName = suffix.Substring(1); var rootTable = new DataTable("@_root"); mDataset.Tables.Add(rootTable); mTableNameToTypeName[rootTable.TableName] = curType.ToString(); if (curType == null) { return(mDataset); } var column = new DataColumn(); column.DataType = typeof(string); column.ColumnName = "@filename"; column.Caption = "文件名"; column.ReadOnly = false; column.Unique = false; rootTable.Columns.Add(column); _InitColumn(curType, rootTable, null); var keys = new Stack <ParentKeysInfo>(); var props = curType.GetProperties(); foreach (var file in files) { var dotIdx = file.LastIndexOf('.'); if (dotIdx < 0) { continue; } if (file.Substring(dotIdx) != suffix) { continue; } var obj = Form1.Inst.m_CSCommonAssembly.CreateInstance(curType.ToString()); ServerFrame.Config.IConfigurator.FillProperty(obj, file); var row = rootTable.NewRow(); row["@filename"] = file.Substring(file.LastIndexOf('\\') + 1); //mCurFileName = row["@filename"].ToString(); ParentKeysInfo key = new ParentKeysInfo(); key.KeyName = "@filename"; key.Descrip = "文件名"; key.value = row["@filename"]; keys.Push(key); _FillRow(obj, row, props, -1, keys, null); rootTable.Rows.Add(row); keys.Pop(); } return(mDataset); }
public bool FillObject(object obj, System.Data.DataRow row, Stack <ParentKeysInfo> parentKeys, string parentProName) { Type objType = obj.GetType(); var props = objType.GetProperties(); foreach (var p in props) { if (p.PropertyType.IsGenericType) { if (row.Table.DataSet.Tables.IndexOf("@" + p.Name) < 0) { continue; } var subType = p.PropertyType.GetGenericArguments()[0]; System.Reflection.MethodInfo addPro = p.PropertyType.GetMethod("Add"); if (addPro == null) { throw new ArgumentException(string.Format("类型错误,{0}", p.PropertyType.ToString())); } string tbName = "@" + p.Name; string parentTbName = ""; foreach (var k in parentKeys) { parentTbName += k.parentTableName + "-"; } if (parentKeys.Count > 0) { parentTbName = parentTbName.Remove(parentTbName.Length - 1); } tbName = _GetRepeatTableName(tbName, parentTbName); var subTable = row.Table.DataSet.Tables[tbName]; //var mainkeyPro = objType.GetProperty(mTableNameToMainKey[row.Table.TableName]); string sqlCondition = ""; bool needSql = true; foreach (var info in parentKeys) { if (info.valueType == typeof(string)) { sqlCondition += string.Format("{0}='{1}' and ", info.KeyName, info.value); } else { sqlCondition += string.Format("{0}='{1}' and ", info.KeyName, info.value); } if (subTable.Columns.IndexOf(info.KeyName) < 0) { needSql = false; } } if (needSql) { sqlCondition = sqlCondition.Remove(sqlCondition.LastIndexOf("and ")); DataRow[] subDataRows = subTable.Select(sqlCondition); var listObj = p.GetValue(obj, null); foreach (var subrow in subDataRows) { object subObj = Form1.Inst.m_ServerCommonAssembly.CreateInstance(subType.ToString()); if (subObj == null && subType.IsSubclassOf(typeof(System.ValueType))) { _AddPropertyInvoke(subType, addPro, listObj, subrow); continue; } ParentKeysInfo key = new ParentKeysInfo(); key.KeyName = subrow.Table.TableName + "_index"; key.value = subrow["@index"]; key.valueType = typeof(int); key.parentTableName = subrow.Table.TableName; parentKeys.Push(key); FillObject(subObj, subrow, parentKeys, null); addPro.Invoke(listObj, new object[] { subObj }); parentKeys.Pop(); } } } if (p.PropertyType.IsSubclassOf(typeof(System.ValueType)) || p.PropertyType == typeof(string)) { _SetObjValue(obj, row, p, parentProName); } else { if (row.Table.Columns.IndexOf(p.Name) < 0) { List <string> pNames = new List <string>(); foreach (DataColumn col in row.Table.Columns) { var s = col.ColumnName.Split('-'); if (s.Length < 2) { continue; } pNames.AddRange(s); pNames.Remove(s[0]); } if (pNames.IndexOf(p.Name) < 0) { continue; } } var sub = p.GetValue(obj, null); FillObject(sub, row, parentKeys, p.Name); } } return(true); }
public void _FillRow(object obj, DataRow row, System.Reflection.PropertyInfo[] props, int index, Stack <ParentKeysInfo> parentKeys, string parentFeildName) { foreach (var pro in props) { if (pro.CanWrite == false) { continue; } if (pro.PropertyType.IsGenericType) { var tbName = "@" + pro.Name; var subType = pro.PropertyType.GetGenericArguments()[0]; string parentName = ""; if (mTableNameToOwnerTabel.ContainsKey(row.Table.TableName)) { parentName = row.Table.TableName + "-" + mTableNameToOwnerTabel[row.Table.TableName]; } else { parentName = row.Table.TableName; } DataTable subTb; if (!mDataset.Tables.Contains(tbName)) { subTb = _CreateDT(parentKeys, pro, tbName, subType, parentName); } else { if (mRepeatTableName.ContainsKey(tbName)) {//有重复的,那名字就不能简单判断 string reName = ""; foreach (string tn in mTableNameToOwnerTabel.Keys) { if (tbName.Length > tn.Length) { continue; } if (tn.Substring(0, tbName.Length) != tbName) { continue; } if (mTableNameToOwnerTabel[tn] == parentName) { reName = tn; break; } } if (reName == "") {//说明这个父没出现过,是新的 } else {//已经搞过的表 tbName = reName; } } if (mTableNameToOwnerTabel.ContainsKey(tbName) && mTableNameToOwnerTabel[tbName] == parentName) {//表名和父级表明一致,代表已经创建过一样的表 subTb = mDataset.Tables[tbName]; if (subTb == null) { } } else {//表名重复,但是父级表名不一样 if (mRepeatTableName.ContainsKey(tbName)) { mRepeatTableName[tbName] += 1; } else { mRepeatTableName[tbName] = 1; } tbName += "-" + mRepeatTableName[tbName].ToString(); //加上区分 subTb = _CreateDT(parentKeys, pro, tbName, subType, parentName); } } System.Reflection.PropertyInfo pi = pro.PropertyType.GetProperty("Count"); if (pi == null) { throw new ArgumentException(string.Format("类型错误,{0}", pro.PropertyType.ToString())); } System.Reflection.PropertyInfo itemPro = pro.PropertyType.GetProperty("Item"); if (itemPro == null) { throw new ArgumentException(string.Format("类型错误,{0}", pro.PropertyType.ToString())); } var listObj = pro.GetValue(obj, null); int count = (int)pi.GetValue(listObj, null); for (int i = 0; i < count; i++) { var subObj = itemPro.GetValue(listObj, new object[] { i }); var subRow = subTb.NewRow(); foreach (var pkey in parentKeys) { subRow[pkey.KeyName] = pkey.value; } if (subType.IsSubclassOf(typeof(System.ValueType))) { subRow["@value"] = subObj; } var parent = parentKeys.Peek(); ParentKeysInfo keyInfo = new ParentKeysInfo(); parentKeys.Push(keyInfo); keyInfo.Descrip = subRow.Table.TableName + "_序号"; keyInfo.value = i; keyInfo.KeyName = subRow.Table.TableName + "_index"; _FillRow(subObj, subRow, subType.GetProperties(), i, parentKeys, null); subRow["@index"] = i; subTb.Rows.Add(subRow); parentKeys.Pop(); } continue; } var attrs = pro.GetCustomAttributes(typeof(ServerFrame.Config.DataValueAttribute), true); if (attrs == null) { continue; } if (attrs.Length <= 0) { continue; } if (pro.PropertyType.IsSubclassOf(typeof(System.ValueType)) || pro.PropertyType == typeof(string)) { object value = pro.GetValue(obj, null); if (parentFeildName == null) { row[pro.Name] = value; } else { row[pro.Name + "-" + parentFeildName] = value; } } else { var sub = pro.GetValue(obj, null); _FillRow(sub, row, pro.PropertyType.GetProperties(), index, parentKeys, pro.Name); } } }