public void T() { Unit u = Unit.FindByName(Unit.Cm); TestDeviceData dat = new TestDeviceData(); PropertyInfo[] pis = dat.GetType().GetProperties(); foreach (PropertyInfo pi in pis) { object[] atts = pi.GetCustomAttributes(typeof(DataItemAttribute), false); if (atts.Length > 0) { //Console.WriteLine(atts[0].ToString()); DataItemAttribute att = (DataItemAttribute)atts[0]; object value = pi.GetValue(dat, null); Console.WriteLine("{0} {1} {2} {3}", att.Name, att.OrderNumber, value, att.Unit.Text); } } }
/// <summary> /// /// </summary> /// <param name="datas"></param> /// <returns></returns> private DataTable ConvertToDataTable(DataCollection datas) { DataTable tbl = new DataTable(); if (datas.Count > 0) { IData last = datas[datas.Count - 1]; Type lastType = last.GetType(); //ReportItemCollection reportItems = last.GetReportItems(); //foreach (ReportItem ri in reportItems) //{ // Type valueType = ri.Value.GetType(); // DataColumn column = new DataColumn(ri.Name, valueType); // tbl.Columns.Add(column); //} //foreach (IData data in datas) //{ // if (data.GetType() != lastType) // { // continue; // } // object[] values = GetReportItemCollectionValues(data.GetReportItems()); // tbl.Rows.Add(values); //} AttributePropertyInfoPairCollection ss = last.GetDeviceDataItemAttributes(); foreach (AttributePropertyInfoPair s in ss) { Type valueType = s.PropertyInfo.PropertyType; DataItemAttribute diAttribute = s.Attribute; string columnName = diAttribute.Name; DataColumn column = new DataColumn(columnName, valueType); column.ExtendedProperties["unit"] = diAttribute.Unit.Text; column.ExtendedProperties["format"] = diAttribute.Format; column.ExtendedProperties["name"] = diAttribute.Name; tbl.Columns.Add(column); } foreach (IData data in datas) { if (data.GetType() != lastType) { continue; } object[] values = new object[ss.Count]; int idx = 0; foreach (AttributePropertyInfoPair s in ss) { object v = s.PropertyInfo.GetValue(data, null); values[idx++] = v; } tbl.Rows.Add(values); } } return(tbl); }