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(); } }
/// <summary> /// 检查类型是否满足绑定,并且检查绑定来源于配置或者代码 /// </summary> /// <param name="t"></param> /// <returns></returns> private bool isMatchBinding(Type t) { _tableAttr = me.GetCustomAttribute <DataBindingAttribute>(true); if (_tableAttr != null) { setBindingMode(_tableAttr); return(true); } return(false); }
public void DataBinding() { dba = me.GetCustomAttributes(typeof(DataBindingAttribute), true).FirstOrDefault() as DataBindingAttribute; if (dba != null) { dba.TableName = string.IsNullOrEmpty(dba.TableName) ? me.Name : dba.TableName; if (TypeCounts.ContainsKey(me)) { TypeCounts[me] += 1; } else { TypeCounts.Add(me, 0); } List <Tuple <MemberInfo, OrderAttribute> > reflectionMembers = null; IEnumerable <MemberInfo> members = null; switch (dba.SettingMode) { case SettingType.PropertyOnly: members = me.GetProperties(); break; case SettingType.MethodOnly: members = me.GetMethods(); break; default: members = me.GetMembers(); break; } reflectionMembers = members .Where(m => m.GetCustomAttributes(typeof(OrderAttribute), true).FirstOrDefault() != null) .Select(m => { return(new Tuple <MemberInfo, OrderAttribute>(m, m.GetCustomAttributes(typeof(OrderAttribute), true).FirstOrDefault() as OrderAttribute)); }).OrderBy(m => m.Item2.Order).ToList(); if (IsUsingSampleData) { sampleDataBinding(reflectionMembers); } else { tableDataBinding(reflectionMembers); } } }
private void setBindingMode(DataBindingAttribute tableAttr) { if (GlobalBindingMode == null) { _bindingMode = new BindingMode() { DataMode = tableAttr.DataMode, LoopMode = tableAttr.LoopMode, RecusionMode = tableAttr.RecusionMode, SettingMode = tableAttr.SettingMode }; } else { _bindingMode = GlobalBindingMode; } }
private void runRecusion(object returnObj) { if (_bindingMode.RecusionMode == RecusionType.Recusion) { if (returnObj != null) { Type returnType = returnObj.GetType(); if (returnType.IsClass && returnType != typeof(string)) { Tuple <object, Type, DataBindingAttribute> status = new Tuple <object, Type, DataBindingAttribute>( _tempInstance, me, _tableAttr); _objStatus.Push(status); Update(returnObj); status = _objStatus.Pop(); _tempInstance = status.Item1; me = status.Item2; _tableAttr = status.Item3; } } } }
public void DataBinding(Tuple <string, BindingType> BindingMember) { dba = me.GetCustomAttributes(typeof(DataBindingAttribute), true).FirstOrDefault() as DataBindingAttribute; if (dba != null) { dba.TableName = string.IsNullOrEmpty(dba.TableName) ? me.Name : dba.TableName; MemberInfo mi = null; switch (BindingMember.Item2) { case BindingType.Method: mi = me.GetMethod(BindingMember.Item1); break; default: mi = me.GetProperty(BindingMember.Item1); break; } if (mi != null) { OrderAttribute oa = mi.GetCustomAttributes(typeof(OrderAttribute), true).FirstOrDefault() as OrderAttribute; if (oa != null) { List <Tuple <MemberInfo, OrderAttribute> > memberList = new List <Tuple <MemberInfo, OrderAttribute> >() { new Tuple <MemberInfo, OrderAttribute>(mi, oa) }; if (IsUsingSampleData) { sampleDataBinding(memberList); } else { tableDataBinding(memberList); } } } } }