private void setComplexProperty(PropertyInfo prop, ColumnBindingAttribute attribute, DataRow[] rows, int index) { object value = null; value = getConvertValue(attribute, rows, prop.PropertyType); setValue(prop, value, attribute); }
/// <summary> /// if column binding attribute has no column name, using the property as column name /// </summary> /// <param name="prop"></param> /// <param name="attr"></param> private void resetColumnName(MemberInfo prop, ColumnBindingAttribute attr) { if (attr.ColNames == null || attr.ColNames.Count() == 0) { attr.ColNames = new string[] { prop.Name }; } }
private void invokeSampleMethod(MethodInfo method, OrderAttribute attr) { if (method.GetParameters().Count() == 0) { dynamic returnObj = method.Invoke(_tempInstance, null); if (isSimpleField(method.ReturnType)) { ColumnBindingAttribute colAttr = attr as ColumnBindingAttribute; if (colAttr != null && colAttr.Directory == DataDirectory.Output) { resetColumnName(method, colAttr); if (_sampleDic.ContainsKey(colAttr.ColNames[0])) { _sampleDic[colAttr.ColNames[0]] = returnObj; } else { _sampleDic.Add(colAttr.ColNames[0], returnObj); } } } else if (method.ReturnType.IsClass) { runRecusion(returnObj); } } }
//private void tableDataBinding(List<Tuple<MemberInfo, OrderAttribute>> mos) //{ // foreach (var mo in mos) // { // if (mo.Item1 is PropertyInfo) // { // PropertyInfo prop = mo.Item1 as PropertyInfo; // ColumnBindingAttribute colAttr = mo.Item2 as ColumnBindingAttribute; // resetColumnName(mo.Item1, colAttr); // bool isSimpleProp = isSimpleField(prop); // DataRow[] data = null; // if (_bindingMode.DataMode == DataType.FromShareTable) // { // data = getSharedData(colAttr); // } // else // { // data = getPrivateData(colAttr); // } // int index = _bindingMode.LoopMode == LoopType.Loop ? TypeCounts[me] : 0; // if (isSimpleProp) // { // if (colAttr.Directory == DataDirectory.Input) // setSingleProperty(prop, colAttr, data, index); // else // getSingleProperty(prop, colAttr, data, index); // } // else // { // if (colAttr.Directory == DataDirectory.Input) // setComplexProperty(prop, colAttr, data, index); // } // } // else if (mo.Item1 is MethodInfo) // { // invokeMethod(mo.Item1 as MethodInfo); // } // } //} private DataRow[] getSharedData(ColumnBindingAttribute attribute) { DataTable dt = null; string tableName = ""; string columnName = attribute.ColNames.Last(); foreach (var dic in _columnTableMappings) { if (dic.ContainsKey(columnName.ToLower())) { tableName = dic[columnName.ToLower()]; } } if (tableName != "" && _ds.Tables.Contains(tableName)) { dt = _ds.Tables[tableName]; } if (dt != null) { string filter = null; filter = getTableFilter(attribute); if (filter != null && isColumnsInclude(dt, attribute)) { return(dt.Select(filter)); } } return(null); }
private void invokeMethod(MethodInfo method, OrderAttribute attr) { if (method.GetParameters().Count() == 0) { dynamic returnObj = method.Invoke(_tempInstance, null); if (isSimpleField(method.ReturnType)) { ColumnBindingAttribute colAttr = attr as ColumnBindingAttribute; if (colAttr != null && colAttr.Directory == DataDirectory.Output) { resetColumnName(method, colAttr); DataRow[] data = null; if (_bindingMode.DataMode == DataType.FromShareTable) { data = getSharedData(colAttr); } else { data = getPrivateData(colAttr); } int index = _bindingMode.LoopMode == LoopType.Loop ? TypeCounts[me] : 0; getSingleProperty(method, returnObj, colAttr, data, index); } } else if (method.ReturnType.IsClass) { runRecusion(returnObj); } } }
private void getSingleProperty(PropertyInfo prop, ColumnBindingAttribute attribute, DataRow[] rows, int index) { string colName = attribute.ColNames.First(); DataRow dr = rows[index]; dr[colName] = prop.GetValue(this, null); }
private DataRow[] getSharedData(ColumnBindingAttribute attribute, bool isSimpleCondition) { DataTable dt = null; string tableName = ""; string columnName = attribute.ColNames.Last(); if (_tableMapping.ContainsKey(columnName.ToLower())) { tableName = _tableMapping[columnName.ToLower()]; } if (tableName != "" && Data.Tables.Contains(tableName)) { dt = Data.Tables[tableName]; } if (dt != null) { string filter = null; filter = getTableFilter(attribute); if (filter != null && isColumnsInclude(dt, attribute)) { return(dt.Select(filter)); } } return(null); }
private string getTableFilter(ColumnBindingAttribute attribute) { string filter = null; if (attribute != null) { filter = _tableAttr.IdColumnName + "=" + CurrentId; } return(filter); }
private void btn_Create_Click(object sender, RoutedEventArgs e) { DataSet ds = new DataSet(); DataTable shareTable = new DataTable("Shared"); ds.Tables.Add(shareTable); shareTable.Columns.Add("Id"); DataRow dr = shareTable.Rows.Add("1"); foreach (var t in selectedTypes) { DataBindingAttribute dba = t.GetCustomAttribute <DataBindingAttribute>(); string tableName = dba.TableName == null ? t.Name : dba.TableName; foreach (var prop in t.GetProperties().Where(p => p.GetCustomAttribute(typeof(ColumnBindingAttribute), true) != null)) { ColumnBindingAttribute attr = prop.GetCustomAttribute(typeof(ColumnBindingAttribute), true) as ColumnBindingAttribute; if (attr.ColNames == null || attr.ColNames.Count() < 2) { string colName = attr.ColNames == null ? prop.Name : attr.ColNames.First(); if (!shareTable.Columns.Contains(colName)) { shareTable.Columns.Add(colName); SingleSampleDataAttribute sampleAttr = prop.GetCustomAttribute(typeof(SingleSampleDataAttribute), true) as SingleSampleDataAttribute; if (sampleAttr != null) { shareTable.Rows[0][colName] = sampleAttr.Value; } } } else { DataTable newTable = null; if (!ds.Tables.Contains(tableName)) { newTable = new DataTable(tableName); ds.Tables.Add(newTable); } if (newTable != null) { foreach (var s in attr.ColNames) { newTable.Columns.Add(s); } } } } ExcelHelper.Current.Create(@"C:\Demo\1.xlsx"); foreach (DataTable dt in ds.Tables) { ExcelHelper.Current.Write(dt); } ExcelHelper.Current.Close(); } }
private void getSingleProperty(MemberInfo mi, object value, ColumnBindingAttribute attribute, DataRow[] rows, int index) { string colName = attribute.ColNames.First(); DataRow dr = rows[index]; //prop.GetValue(_tempInstance, null); dr[colName] = value; if (OnGettingProperty != null) { OnGettingProperty(_tempInstance, new SetPropertyArgs(mi, dr[colName], attribute)); } }
private void setValue(PropertyInfo prop, object value, ColumnBindingAttribute attribute) { if (value != null) { prop.SetValue(_tempInstance, value, null); if (OnSettingProperty != null) { OnSettingProperty(_tempInstance, new SetPropertyArgs(prop, value, attribute)); } runRecusion(value); } }
private void tableBinding(List <Tuple <MemberInfo, OrderAttribute> > mos) { foreach (var mo in mos) { if (mo.Item1 is PropertyInfo) { PropertyInfo prop = mo.Item1 as PropertyInfo; ColumnBindingAttribute colAttr = mo.Item2 as ColumnBindingAttribute; resetColumnName(mo.Item1, colAttr); bool isSimpleProp = isSimpleField(prop.PropertyType); DataRow[] data = null; if (_bindingMode.DataMode == DataType.FromShareTable) { data = getSharedData(colAttr); } else { data = getPrivateData(colAttr); } int index = _bindingMode.LoopMode == LoopType.Loop ? TypeCounts[me] : 0; if (isSimpleProp) { if (colAttr.Directory == DataDirectory.Input) { setSingleProperty(prop, colAttr, data, index); } else { getSingleProperty(prop, prop.GetValue(_tempInstance, null), colAttr, data, index); } } else { if (colAttr.Directory == DataDirectory.Input) { setComplexProperty(prop, colAttr, data, index); } } } else if (mo.Item1 is MethodInfo) { invokeMethod(mo.Item1 as MethodInfo, mo.Item2); } } }
private bool isColumnsInclude(DataTable dt, ColumnBindingAttribute attribute) { bool isAllContain = true; foreach (var s in attribute.ColNames) { if (dt.Columns.Cast <DataColumn>().Where(c => c.ColumnName.ToLower().Contains(s.ToLower())).FirstOrDefault() == null) { isAllContain = false; break; } } return(isAllContain); }
private object getConvertValue(ColumnBindingAttribute colAttr, object SourceValue, Type targetType) { object value = null; value = Convert.ChangeType(SourceValue, targetType); //if (colAttr.MethodName != null && colAttr.Target != null) //{ // var customDelegate = Delegate.CreateDelegate(typeof(ColumnBindingConvert), colAttr.Target, colAttr.MethodName) as ColumnBindingConvert; // value = customDelegate.Invoke(SourceValue); //} //else //{ // value = Convert.ChangeType(SourceValue, targetType); //} return(value); }
private void setSingleProperty(PropertyInfo prop, ColumnBindingAttribute attribute, DataRow[] rows, int index) { object value = null; if (rows != null && rows.Count() > 0) { string colName = attribute.ColNames.First(); //May Throw Error DataRow dr = rows[index]; if (dr[colName] != null && dr[colName].ToString() != "") { value = getConvertValue(attribute, dr[colName], prop.PropertyType); } } setValue(prop, value, attribute); }
private void setProperty(PropertyInfo prop, ColumnBindingAttribute attribute, int index) { var propertyType = prop.PropertyType; object value = null; var singleData = prop.GetCustomAttributes(typeof(SingleSampleDataAttribute), true).Cast <SingleSampleDataAttribute>().Where(d => d.Group == index).FirstOrDefault(); if (singleData != null) { value = getConvertValue(attribute, singleData.Value, propertyType); } else { var datas = prop.GetCustomAttributes(typeof(ComplexSampleDataAttribute), true).Cast <ComplexSampleDataAttribute>(); if (datas.Count() > 0) { var header = datas.Where(c => c.DataType == SampleDataType.Header && c.Group == index).FirstOrDefault(); if (header != null) { DataTable dt = new DataTable(); foreach (var s in header.Content) { dt.Columns.Add(new DataColumn(s)); } var body = datas.Where(c => c.DataType == SampleDataType.Body && c.Group == index); foreach (var r in body) { var row = dt.NewRow(); for (int i = 0; i < r.Content.Count(); i++) { row[i] = r.Content[i]; } dt.Rows.Add(row); } var rows = dt.Select(); value = getConvertValue(attribute, rows, propertyType); } } } setValue(prop, value, attribute); }
private DataRow[] getPrivateData(ColumnBindingAttribute attribute, bool isSimpleCondition) { DataTable dt = null; if (Data.Tables.Contains(dba.TableName)) { dt = Data.Tables[dba.TableName]; } if (dt != null) { string filter = null; filter = getTableFilter(attribute); if (filter != null && isColumnsInclude(dt, attribute)) { return(dt.Select(filter)); } } return(null); }
private DataRow[] getPrivateData(ColumnBindingAttribute attribute) { DataTable dt = null; if (_ds.Tables.Contains(_tableAttr.TableName)) { dt = _ds.Tables[_tableAttr.TableName]; } if (dt != null) { string filter = null; filter = getTableFilter(attribute); if (filter != null && isColumnsInclude(dt, attribute)) { return(dt.Select(filter)); } } return(null); }
public SetPropertyArgs(MemberInfo Member, object value, ColumnBindingAttribute Attribute) { this.Member = Member; this.Value = value; this.Attribute = Attribute; }