public static object[] ToObjectArray(this object oSrc) { if (oSrc == null) { return(new object[0]); } var oResult = new List <object>(); oSrc.Traverse((oInstance, oPropertyInfo) => { object[] pkAttrList = oPropertyInfo.GetCustomAttributes(typeof(PKAttribute), false); if (pkAttrList.Length < 1) { oResult.Add(oPropertyInfo.GetValue(oInstance)); return; } // if no PK configured PKAttribute pk = (PKAttribute)pkAttrList[0]; // Primary key which is identity is not inserted into output column list // because such field usage is intended for saving a new item but identity // column is filled by DB. if (!pk.WithIdentity) { oResult.Add(oPropertyInfo.GetValue(oInstance)); } }); return(oResult.ToArray()); } // ToObjectArray
} // CreateTableParameter public virtual QueryParameter CreateTableParameter( Type oColumnInfo, string sFieldName, IEnumerable oValues, Func <object, object[]> oValueToRow, IEnumerable <Type> oCustomTypeOrder = null ) { var tbl = new DataTable(); if (TypeUtils.IsSimpleType(oColumnInfo)) { AddColumn(tbl, oColumnInfo); } else { if (oCustomTypeOrder == null) { PropertyTraverser.Traverse(oColumnInfo, (i, oPropertyInfo) => { object[] pkAttrList = oPropertyInfo.GetCustomAttributes(typeof(PKAttribute), false); if (pkAttrList.Length < 1) { AddColumn(tbl, oPropertyInfo.PropertyType); return; } // if no PK configured PKAttribute pk = (PKAttribute)pkAttrList[0]; // Primary key which is identity is not inserted into output column list // because such field usage is intended for saving a new item but identity // column is filled by DB. if (!pk.WithIdentity) { AddColumn(tbl, oPropertyInfo.PropertyType); } }); } else { foreach (Type t in oCustomTypeOrder) { AddColumn(tbl, t); } } // if } // if if (oValues != null) { foreach (object v in oValues) { tbl.Rows.Add(oValueToRow(v)); } } return(BuildTableParameter(sFieldName, tbl)); } // CreateTableParameter
public ReflectDataMapper(Type klass, string connStr) : base(connStr) { this.klass = klass; ClassProperties classproperties = new ClassProperties(); TableAttribute att = (TableAttribute)klass.GetCustomAttribute(typeof(TableAttribute), false); tableName = att.Name; List <string> propertyList = new List <string>(); foreach (var p in klass.GetProperties()) { string propertyName = p.Name; PKAttribute pk = (PKAttribute)p.GetCustomAttribute(typeof(PKAttribute)); if (pk == null) { Type propertyType = p.PropertyType; foreach (var property in propertyType.GetProperties()) //There should be an if to only iterate through non primitive nd string properties { PKAttribute propertyPk = (PKAttribute)property.GetCustomAttribute(typeof(PKAttribute)); if (propertyPk != null) { propertyName = property.Name; } } propertyList.Add(propertyName); //classproperties.otherProperties.Add(p); } else { idField = p.Name; classproperties.id = p; } } columns = string.Join(",", propertyList); string className = klass.Name; if (!discovery.ContainsKey(className)) { discovery.Add(className, classproperties); } }
internal static void Init(IEnumerable <OriginalBulkCopy> list, string connStr) { _dataTable = list.FirstOrDefault().TableStructure(); _tableName = list.FirstOrDefault().TableStructure().TableName; _connStr = connStr; // Get item Type Type type = list.FirstOrDefault().GetType(); _itemType = type; foreach (var t in type.GetRuntimeProperties()) { _tableIDAttribute = t.GetCustomAttribute <PKAttribute>(); if (_tableIDAttribute != null) { _idProp = t; break; } } ; }