F
fig000
HI,
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<T> GenList,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
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<T> GenList,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