public virtual string GetChampId() { string strChampId = null; if (!m_dicChampsId.TryGetValue(GetType(), out strChampId)) { MemoryTableAttribute attr = GetType().GetCustomAttributes(typeof(MemoryTableAttribute), true)[0] as MemoryTableAttribute; strChampId = attr.ChampId; m_dicChampsId[GetType()] = strChampId; } return(strChampId); }
public virtual string GetNomTable() { string strNomTable = null; if (!m_dicNomsTables.TryGetValue(GetType(), out strNomTable)) { MemoryTableAttribute attr = GetType().GetCustomAttributes(typeof(MemoryTableAttribute), true)[0] as MemoryTableAttribute; strNomTable = attr.NomTable; m_dicNomsTables[GetType()] = strNomTable; } return(strNomTable); }
//////////////////////////////////////////////////////////////////////////// private void RegisterAssembly(Assembly ass) { foreach (Type tp in ass.GetTypes()) { object[] attrs = tp.GetCustomAttributes(typeof(MemoryTableAttribute), true); if (attrs.Length > 0) { MemoryTableAttribute att = attrs[0] as MemoryTableAttribute; m_dicPrivateNomTableToType[att.NomTable] = tp; m_dicPrivateTypeToNomTable[tp] = att.NomTable; } } }
//--------------------------------------- public bool CreateTable(Type type) { MemoryTableAttribute tableAttr = type.GetCustomAttributes(typeof(MemoryTableAttribute), true)[0] as MemoryTableAttribute; string strNomTable = tableAttr.NomTable; string strChampId = tableAttr.ChampId; m_dicPrivateNomTableToType[strNomTable] = type; m_dicPrivateTypeToNomTable[type] = strNomTable; DataTable table = Tables[strNomTable]; if (table != null) { return(true); } table = new DataTable(strNomTable); //Création du champ Id DataColumn col = new DataColumn(strChampId, typeof(string)); col.AllowDBNull = true; table.Columns.Add(col); table.PrimaryKey = new DataColumn[] { col }; //Création des champs de propriété PropertyInfo[] proprietes = type.GetProperties(); foreach (PropertyInfo prop in proprietes) { object[] attrs = prop.GetCustomAttributes(typeof(MemoryFieldAttribute), true); if (attrs != null && attrs.Length > 0) { MemoryFieldAttribute field = attrs[0] as MemoryFieldAttribute; string strNomChamp = field.NomChamp; if (strNomChamp == "") { strNomChamp = prop.Name; } bool bNullable = prop.PropertyType.IsClass; Type tpProp = prop.PropertyType; if (!bNullable) { if (prop.PropertyType.IsGenericType) { if (prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { tpProp = prop.PropertyType.GetGenericArguments()[0]; } } } if (table.Columns[strNomChamp] == null) { col = new DataColumn(strNomChamp, tpProp); col.AllowDBNull = true; table.Columns.Add(col); } } } Tables.Add(table); //Création des relations parentes foreach (PropertyInfo prop in proprietes) { object[] attrs = prop.GetCustomAttributes(typeof(MemoryParentAttribute), true); if (attrs != null && attrs.Length > 0) { MemoryParentAttribute relation = attrs[0] as MemoryParentAttribute; DataTable tableParente = GetTable(prop.PropertyType); string strNomChamp = relation.NomChampFils; if (strNomChamp == "") { strNomChamp = tableParente.PrimaryKey[0].ColumnName; } col = new DataColumn(strNomChamp, typeof(string)); col.AllowDBNull = true; table.Columns.Add(col); DataRelation dtRelation = new DataRelation(GetNomRelation(table.TableName, prop.Name), tableParente.PrimaryKey[0], col, true); Relations.Add(dtRelation); dtRelation.ExtendedProperties[c_ExtPropRelationNomProprieteFils] = prop.Name; foreach (Constraint cst in table.Constraints) { ForeignKeyConstraint fk = cst as ForeignKeyConstraint; if (fk != null && fk.ConstraintName == dtRelation.RelationName) { fk.DeleteRule = relation.IsComposition?Rule.Cascade:Rule.None; } } } } ///Création des tables filles foreach (PropertyInfo prop in proprietes) { object[] attrs = prop.GetCustomAttributes(typeof(MemoryChildAttribute), true); if (attrs != null && attrs.Length > 0) { Type tpFils = prop.PropertyType.GetGenericArguments()[0]; GetTable(tpFils); } } if (!table.Columns.Contains(c_champIsToRead)) { DataColumn colIsToRead = new DataColumn(c_champIsToRead, typeof(bool)); colIsToRead.DefaultValue = false; colIsToRead.AllowDBNull = false; table.Columns.Add(colIsToRead); } return(true); }