我想建立一個通用的方法將excel導出到列表。如果屬性應該打印,則對象將具有屬性。即:反射,屬性和屬性選擇
public class someObject {
public int DontPrint {get; set;}
[ExcelAttributes(PrintMe = true)]
public int PrintMe {get; set;}
[ExcelAttributes(PrintMe = true)]
public int PrintMeToo {get; set;}
}
我需要一個通用的方式來檢查列表並返回一個可打印的對象。像下面這樣。
public AppendCell<T>(List<T> list)
var obj = list[0];
PropertyInfo[] propertyInfos;
propertyInfos = obj.GetType().GetProperties(BindingFlags.Public |
BindingFlags.Instance);
foreach (T list1 in list)
{
foreach (PropertyInfo info in propertyInfos)
{
object[] customAttr = info.GetCustomAttributes(true);
// create cell with data
foreach (object o in customAttr)
{
ExcelAttributes ea = o as ExcelAttributes;
if (ea != null && ea.PrintMe ==true)
Cell c = new Cell(info.GetValue(list1,null).ToString())
}
}
}
return c;
}
所以...我基本上要能夠檢查對象的列表,獲取基於屬性的值可打印性能和打印值的打印屬性。
如果我們創建someObject與價值觀
{DontPrint = 0, PrintMe = 1, PrintMeToo = 2}
{DontPrint = 0, PrintMe = 4, PrintMeToo = 5}
{DontPrint = 0, PrintMe = 3, PrintMeToo = 8}
我希望看到一個列表:
1 2
4 5
3 8
代碼類似於張貼做什麼,我需要的。是否有更簡潔的方式來獲取具有PrintMe屬性的屬性列表,然後遍歷列表並對這些屬性執行操作?
那是一個很好的解決方案。我已經使用了反射屬性。我將一個List傳遞給print類,它使用反射(自定義屬性)來查看哪個屬性意味着顯示。不過,我很好奇你怎麼用界面來做呢? –
Houman
2012-03-12 17:54:28