有一个方法A返回一个List<T>对象,在另一个方法B中接收,方法B中不能直接得到List<T>中T的类型,但却得遍历这个List<T>对象
问题:
能不能在B中通过反射获得T的对象类型?据说这是不可以的,因为在编绎期间T只作为一个占位符,在运行期间由具体类型替换。但如果这样的话,怎么遍历这个返回的List<T>对象?自带的DropDownList的DataSource可以为设定为List<T>,它这个应该也是遍历这个数据源,然后绑定的吧。
问题:
能不能在B中通过反射获得T的对象类型?据说这是不可以的,因为在编绎期间T只作为一个占位符,在运行期间由具体类型替换。但如果这样的话,怎么遍历这个返回的List<T>对象?自带的DropDownList的DataSource可以为设定为List<T>,它这个应该也是遍历这个数据源,然后绑定的吧。
解决方案 »
- 用Microsoft.Office.Interop.Excel 导出EXCEL怎么设定它的格式是文本格式啊
- VS2010更改项目文件夹名称
- 如何用最简单的办法在存储过程中产生2005-6-30这种格式的时间值?
- 基于forms的验证机制 的问题
- 编辑DataGrid的数据时,如何对用户输入的数据进行验证?
- DataList 里面空间取值问题 帮个忙谢谢
- 点击链接,提示:是否删除,点击确定,删除,取消,返回!
- 怎样用代码实现网页窗体最大化
- 别人都说简单的问题我怎么就搞不定呢!!tabstrip和mulipage问题,高分相送,
- asp导入excel提示‘找不到可安装的 ISAM’
- gridview获取指定列值的办法
- listView中的LayoutTemplate和ItemTemplate模板在哪个窗口中创建?怎么在智能显示里没有看到象在Gridview一样的编辑模板项?
dropdownlist.DataSource = List<T>;
dropdownlist.DataBind();我也想用这种方式:customContorl.DataSource = List<T>;
然后在CustomContorl里遍历这个List<T>:
protected override void Render(HtmlTextWriter writer)
{
.....
List<ITree> listTree = (List<ITree>)dataSource;
//由于不能确定T的类型,我写了一个ITree的接口。这样一来,
//每次我用这个自定义控件时T的类型都必须继承这个接口,这显然太2了
//我想实现的效果正如自带的dropdownlist那样,只需知道它的DataSource是个List<T>类型就可以了
WriteDropDownTree(listTree, -1);
.....
}
private void WriteDropDownTree<T>(List<T> listTree, int rootID) where T : ITree
{
CreateTree(listTree, rootID);
} private void CreateTree<T>(List<T> listTree, int rootID) where T : ITree
{
foreach (T node in GetChildeNodes(listTree, rootID))
{
sbTree.AppendFormat("<option value=\"{0}\">{1}</option>", node.ID, "..." + node.Name);
CreateSubOption(listTree,node.ID);
}
}
protected void Page_Load(object sender, EventArgs e)
{ List<string> list=AddItem();
ShowItem(list);
} private void ShowItem(List<string> list)
{
foreach (string s in list)
{
Response.Write(s+"<br/>");
}
} private List<string> AddItem()
{
List<string> list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("3");
list.Add("4");
return list;
}
谢谢你的回答,现在的问题不是怎么遍历一个具体类型的List<T>,而是遍历未知具体类型的List<T>的问题
关于T,就可以用反射来处理了。
但是这个T是在运行时动态替换的,这个也可以通过反射得到它类型么?我在网上找了好久资料都没有找到方法,能不能再具体一点?
foreach(T t in listTree)
{
Type itemType = t.GetType();
string s = itemType.GetGenericTypeDefinition().FullName;
}
a[0].GetType();
{
IEnumerable enumerable = collection as IEnumerable;
if (enumerable == null) { throw new Exception("collection必须实现IEnumerable接口"); }
foreach (object item in enumerable)
{
Type t = item.GetType();
/*这个地方你喜欢作什么就作什么啦*/
}
} //或是你干脆可以这样
private void objListForEach(IEnumerable collection)
{
foreach (object item in collection)
{
Type t = item.GetType();
/*这个地方你喜欢作什么就作什么啦*/
}
}
两段小代码,不知道是不是你想要的答案。
{
void SomeMethodCore<T>(List<T> list)
{
// 这个地方你喜欢作什么就作什么,并且知道T的具体类型
foreach(T item in list)
{
Console.WriteLine(item);
}
} // 使用时,调用这个方法
void SomeMethod(object itShouldBeAList)
{
if (itShouldBeAList == null)
throw new ArgumentNullException("itShouldBeAList");
if (!itShouldBeAList.GetType().IsGenericType)
throw new ArgumentException("itShouldBeAList is not a List<T>");
if (itShouldBeAList.GetType().GetGenericTypeDefinition() != typeof(List<>))
throw new ArgumentException("itShouldBeAList is not a List<T>");
typeof(Sample).GetMethod("SomeMethodCore", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(itShouldBeAList.GetType().GetGenericArguments()).Invoke(this, new object[] { itShouldBeAList });
} // 一个模拟的调用
void Caller()
{
object list = new List<int>{1,2,3};
SomeMethod(list);
}
}
看了你的代码之后才发现自己犯了一个很SB的错误,我一直想直接获取List<T>里T的类型,却忘了传说中那句 Object类是所有类的基类再说一次谢谢你,愿上帝与你同在
你的代码刚好是我下一步需要做的,非常感谢