[英]Get properties values from an object generic list c#
我試圖從通用列表的屬性中獲取值,但出現錯誤“ T不包含...的定義”。
var values GetValues(Id);
if (values != null)
{
CreateTable<Object>(values);
}
/////
private void CreateTable<T>(IList<T> array)
{
foreach (item in array)
{
//Problem is here **** when trying to get item.tag
var text = new TextBox(){ Text = item.Tag , ID = item.TagID.ToString() };
}
}
如何使其與泛型一起工作? 感謝任何幫助
為什么為什么期望某個任意T
類型的對象具有Tag
和TagID
屬性? 這些屬性在哪里定義? 如果它們是在接口上定義的,那么說
public interface IItem
{
string Tag { get; }
int TagID { get; }
}
那么您就不需要泛型,可以將CreateTable
重新定義為
private void CreateTable(IList<IITem> array)
{
foreach (var item in array)
{
//Problem is here **** when trying to get item.tag
var text = new TextBox(){ Text = item.Tag , ID = item.TagID.ToString() };
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.