foreach循环和泛型类 [英] foreach loop and generic class

查看:62
本文介绍了foreach循环和泛型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是仿制药的新手。我写了一个简单的类,我通过一个通用列表来获得
。我能够通过列表甚至传递列表的

类型,以便我可以使用它来遍历它。这是一个通用列表

的业务对象。我能够在调试器中看到类型是正确的




但是当我尝试使用类型I遍历列表时不能

编译。我已验证在调试器中正确传递

的相同类型变量给出错误:


类型或命名空间rectype无法找到。


当我尝试在foreach循环中使用它时会发生这种情况。我尝试了实现IEnumerabler的
但是从那里得到了编译错误。这是

代码。正如你所看到的,我已经注意到了令人讨厌的foreach循环。在

调试器中,我可以看到rectype是正确的底层类型,但

我可以在foreach循环中使用它:


使用System;

使用System.Data;

使用System.Configuration;

使用System.Web;

使用System.Web.Security;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System .Web.UI.WebControls.WebParts;

使用System.Web.UI.HtmlControls;

使用System.Collections.Generic;

使用System.Reflection;

///< summary>

/// BusinessObjectsGeneric的摘要描述

///< / summary>

公共类BusinessObjectsGeneric< T>

{

public BusinessObjectsGeneric()

{

//

// TODO:在这里添加构造函数逻辑

//

}


public DataTable ReturnObjectCollectioAsTable(List< TGenList,Type

typ)

{


if(GenList == null)

返回null;

DataTable dt = new DataTable();

PropertyInfo [] pi = typ.GetProperties();

foreach(PropertyInfo p in pi)

{

dt.Columns.Add(new DataColumn(p.Name,p.PropertyType));


}


类型def = typeof(BusinessObjectsGeneric< T>);

类型rectype = typ;


// foreach(GenList中的rectype项目)

// {


//}


返回dt;


}

}


任何帮助都将不胜感激。


Fig000



I''m new to generics. I''ve written a simple class to which I''m
passing a generic list. I''m able to pass the list and even pass the
type of the list so I can use it to traverse it. It''s a generic list
of business objects. I''m able to see that the type is the correct one
in the debugger.

However when I try to traverse the list using the type I can''t
compile. The same type variable I''ve verified as being passed
correctly in the debugger gives the error:

The type or namespace "rectype" could not be found.

This happens when I try to use it in a foreach loop. I tried
implementing IEnumerabler but got compile error from that. Here is the
code. As you can see I''ve commented out the offending foreach loop. In
the debugger I can see that rectype is the correct underlying type but
I can use it in the foreach loop:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// Summary description for BusinessObjectsGeneric
/// </summary>
public class BusinessObjectsGeneric<T>
{
public BusinessObjectsGeneric()
{
//
// TODO: Add constructor logic here
//
}

public DataTable ReturnObjectCollectioAsTable(List<TGenList,Type
typ)
{

if (GenList == null)
return null;
DataTable dt = new DataTable();
PropertyInfo[] pi = typ.GetProperties();
foreach (PropertyInfo p in pi)
{

dt.Columns.Add(new DataColumn(p.Name, p.PropertyType));

}

Type def = typeof(BusinessObjectsGeneric<T>);
Type rectype = typ;

// foreach (rectype item in GenList)
// {

// }

return dt;

}
}

Any help would be appreciated.

Fig000

推荐答案

2007-11-12 16:15:08 -0800,fig000< ne *********** @ yahoo.comsaid:
On 2007-11-12 16:15:08 -0800, fig000 <ne***********@yahoo.comsaid:

HI,


我是仿制药的新手。我写了一个简单的类,我通过一个通用列表来获得
。我能够通过列表甚至传递列表的

类型,以便我可以使用它来遍历它。这是一个通用列表

的业务对象。我能够在调试器中看到类型是正确的




但是当我尝试使用类型I遍历列表时不能

编译。我已验证在调试器中正确传递

的相同类型变量给出错误:


类型或命名空间rectype无法找到。


I''m new to generics. I''ve written a simple class to which I''m
passing a generic list. I''m able to pass the list and even pass the
type of the list so I can use it to traverse it. It''s a generic list
of business objects. I''m able to see that the type is the correct one
in the debugger.

However when I try to traverse the list using the type I can''t
compile. The same type variable I''ve verified as being passed
correctly in the debugger gives the error:

The type or namespace "rectype" could not be found.



那么,你究竟想要做什么?


你得到的错误是因为rectype实际上并不是一种可以使用的类型

用于变量声明。它是Type类型的类实例。


你可以写这样的东西:


foreach(GenList中的T项)

{

}


然后这就引出了一个问题:你将用item做什么?循环内部?


为什么要将Type作为参数传递给方法,以及你真的想用这个参数怎么样?



Pete

Well, what exactly are you actually trying to do?

You get the error because "rectype" isn''t actually a type you can use
for a variable declaration. It''s a class instance of type Type.

You could write something like this:

foreach (T item in GenList)
{
}

but then that begs the question: what will you do with "item" inside the loop?

Why are you passing a Type as a parameter to the method, and how is it
that you really want to use that parameter?

Pete


你有一个类型为T的通用列表:

List< ; TGenList; // =新列表< T>();

此列表的每种语法都是



foreach(GenList中的T项)

{

//用物品做的东西。

}


" fig000" < ne *********** @ yahoo.com写了留言

新闻:11 ******************* *@19g2000hsx.googlegroup s.com ...
You have a generic list of type T:
List<TGenList; // = new List<T>();

for each syntax for this list is:

foreach( T item in GenList )
{
// do stuff with the item.
}

"fig000" <ne***********@yahoo.comwrote in message
news:11********************@19g2000hsx.googlegroup s.com...

HI,


我是仿制药的新手。我写了一个简单的类,我通过一个通用列表来获得
。我能够通过列表甚至传递列表的

类型,以便我可以使用它来遍历它。这是一个通用列表

的业务对象。我能够在调试器中看到类型是正确的




但是当我尝试使用类型I遍历列表时不能

编译。我已验证在调试器中正确传递

的相同类型变量给出错误:


类型或命名空间rectype无法找到。


当我尝试在foreach循环中使用它时会发生这种情况。我尝试了实现IEnumerabler的
但是从那里得到了编译错误。这是

代码。正如你所看到的,我已经注意到了令人讨厌的foreach循环。在

调试器中,我可以看到rectype是正确的底层类型,但

我可以在foreach循环中使用它:


使用System;

使用System.Data;

使用System.Configuration;

使用System.Web;

使用System.Web.Security;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System .Web.UI.WebControls.WebParts;

使用System.Web.UI.HtmlControls;

使用System.Collections.Generic;

使用System.Reflection;


///< summary>

/// BusinessObjectsGeneric的摘要说明

///< ; / summary>

公共类BusinessObjectsGeneric< T>

{

public BusinessObjectsGeneric()

{

//

// TODO:在这里添加构造函数逻辑

//

}

public DataTable ReturnObjectCollectioAsTable(List< TGenList,Type

typ)

{


if(GenList == null)

返回null;


DataTable dt = new DataTable();

PropertyInfo [] pi = typ.GetProperties();


foreach(PropertyInfo p in pi)

{


dt.Columns.Add(new DataColumn(p.Name,p.PropertyType));


}


类型def = typeof(BusinessObjectsGeneric< T>);

类型rectype = typ;


// foreach (GenList中的rectype项目)

// {


//}


return dt;


}

}


任何帮助都将不胜感激。


Fig000


I''m new to generics. I''ve written a simple class to which I''m
passing a generic list. I''m able to pass the list and even pass the
type of the list so I can use it to traverse it. It''s a generic list
of business objects. I''m able to see that the type is the correct one
in the debugger.

However when I try to traverse the list using the type I can''t
compile. The same type variable I''ve verified as being passed
correctly in the debugger gives the error:

The type or namespace "rectype" could not be found.

This happens when I try to use it in a foreach loop. I tried
implementing IEnumerabler but got compile error from that. Here is the
code. As you can see I''ve commented out the offending foreach loop. In
the debugger I can see that rectype is the correct underlying type but
I can use it in the foreach loop:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// Summary description for BusinessObjectsGeneric
/// </summary>
public class BusinessObjectsGeneric<T>
{
public BusinessObjectsGeneric()
{
//
// TODO: Add constructor logic here
//
}

public DataTable ReturnObjectCollectioAsTable(List<TGenList,Type
typ)
{

if (GenList == null)
return null;
DataTable dt = new DataTable();
PropertyInfo[] pi = typ.GetProperties();
foreach (PropertyInfo p in pi)
{

dt.Columns.Add(new DataColumn(p.Name, p.PropertyType));

}

Type def = typeof(BusinessObjectsGeneric<T>);
Type rectype = typ;

// foreach (rectype item in GenList)
// {

// }

return dt;

}
}

Any help would be appreciated.

Fig000



你不需要传入实际的类型。你只需要完全支持Generics这个

方法。请尝试这样做:


public DataTable ReturnObjectCollectionAsTable< T>(List< TGenList)

{

foreach(GenList中的T项) )

{

//代码在这里

}

}


如果你需要确保T是一个实现某种类型的

接口的类,或者扩展某种类型的对象,你可以使用where子句约束函数




public DataTable ReturnObjectCollectionAsTable< T>(List< TGenList)其中T

:IDisposable //或任何其他基类或接口

-

Andrew Faust

andrew [at] andrewfaust.com
http://www.andrewfaust.com

" fig000" < ne *********** @ yahoo.com写了留言

新闻:11 ******************* *@19g2000hsx.googlegroup s.com ...
You don''t need to pass in the actual Type. You just need to make this
method support Generics fully. Try this instead:

public DataTable ReturnObjectCollectionAsTable<T>(List<TGenList)
{
foreach (T item in GenList)
{
//Code Here
}
}

If you need to make sure T is a class that implements a certain type of
interface or extends some type of object you can constrain the function
with a where clause

public DataTable ReturnObjectCollectionAsTable<T>(List<TGenList) where T
: IDisposable //Or any other base classes or Interfaces
--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
"fig000" <ne***********@yahoo.comwrote in message
news:11********************@19g2000hsx.googlegroup s.com...

HI,


我是仿制药的新手。我写了一个简单的类,我通过一个通用列表来获得
。我能够通过列表甚至传递列表的

类型,以便我可以使用它来遍历它。这是一个通用列表

的业务对象。我能够在调试器中看到类型是正确的




但是当我尝试使用类型I遍历列表时不能

编译。我已验证在调试器中正确传递

的相同类型变量给出错误:


类型或命名空间rectype无法找到。


当我尝试在foreach循环中使用它时会发生这种情况。我尝试了实现IEnumerabler的
但是从那里得到了编译错误。这是

代码。正如你所看到的,我已经注意到了令人讨厌的foreach循环。在

调试器中,我可以看到rectype是正确的底层类型,但

我可以在foreach循环中使用它:


使用System;

使用System.Data;

使用System.Configuration;

使用System.Web;

使用System.Web.Security;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System .Web.UI.WebControls.WebParts;

使用System.Web.UI.HtmlControls;

使用System.Collections.Generic;

使用System.Reflection;


///< summary>

/// BusinessObjectsGeneric的摘要说明

///< ; / summary>

公共类BusinessObjectsGeneric< T>

{

public BusinessObjectsGeneric()

{

//

// TODO:在这里添加构造函数逻辑

//

}

public DataTable ReturnObjectCollectioAsTable(List< TGenList,Type

typ)

{


if(GenList == null)

返回null;


DataTable dt = new DataTable();

PropertyInfo [] pi = typ.GetProperties();


foreach(PropertyInfo p in pi)

{


dt.Columns.Add(new DataColumn(p.Name,p.PropertyType));


}


类型def = typeof(BusinessObjectsGeneric< T>);

类型rectype = typ;


// foreach (GenList中的rectype项目)

// {


//}


return dt;


}

}


任何帮助都将不胜感激。


Fig000


I''m new to generics. I''ve written a simple class to which I''m
passing a generic list. I''m able to pass the list and even pass the
type of the list so I can use it to traverse it. It''s a generic list
of business objects. I''m able to see that the type is the correct one
in the debugger.

However when I try to traverse the list using the type I can''t
compile. The same type variable I''ve verified as being passed
correctly in the debugger gives the error:

The type or namespace "rectype" could not be found.

This happens when I try to use it in a foreach loop. I tried
implementing IEnumerabler but got compile error from that. Here is the
code. As you can see I''ve commented out the offending foreach loop. In
the debugger I can see that rectype is the correct underlying type but
I can use it in the foreach loop:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// Summary description for BusinessObjectsGeneric
/// </summary>
public class BusinessObjectsGeneric<T>
{
public BusinessObjectsGeneric()
{
//
// TODO: Add constructor logic here
//
}

public DataTable ReturnObjectCollectioAsTable(List<TGenList,Type
typ)
{

if (GenList == null)
return null;
DataTable dt = new DataTable();
PropertyInfo[] pi = typ.GetProperties();
foreach (PropertyInfo p in pi)
{

dt.Columns.Add(new DataColumn(p.Name, p.PropertyType));

}

Type def = typeof(BusinessObjectsGeneric<T>);
Type rectype = typ;

// foreach (rectype item in GenList)
// {

// }

return dt;

}
}

Any help would be appreciated.

Fig000


这篇关于foreach循环和泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆