Pete,
I'm sorry I've caused you any frustration. I am learning. You might
call this my proof of concept to see if I want to use generics and
reflection for some basic tasks or (as you pointed out) if I want to.
I hope to finish this procedure and then create add some update, and
insert procs so I can work with dot.net controls such as gridview.
Obviously the update, delete and insert procs don't have to be written
with generics but it would be nice to be able to pass data from the
datatable used to work with the gridview and pass it back to the
generic collection I've been using to keep it current.
The following code is as far as I've gotten. There are two lines that
are important here:
This one seems useless but I'll explain it:
object objthinn = item;
I put that there so I could examine the item object while in the
foreach loop. If I put the cursor over the item object I can see that
it knows what the underlying datatype is (a business class that I
passed with multiple properties) and, to my delight, if I click on the
"+" it actually exposes the whole class and shows me the data in the
record.
Here is the line that I'm stuck on and it's commented out because
it doesn't work a:
//row[i] = (T)item[i];
What I'm trying to do here is simply get the data out of each
property in this business class record ifrom my generic list
(genlist). I've tried a number of different techniques to make this
line work including various casting methods but I can't seem to make
it work. Item, uncast, exposes some not very useful properties (at
least as far as I can tell).
So that's it. All I want to do is traverse the properties of the
given T type record from genlist currently being used in the foreach
loop and transfer them to the "row" object. This is much like the url
I sent you yesterday.
Here is the whole thing. Again I apologize if I've been unclear and
frustrating. I am really starting from scratch with this:
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)
{
if (GenList == null)
return null;
DataTable dt = new DataTable();
PropertyInfo[] pi = typeof(T).GetProperties();
foreach (PropertyInfo p in pi)
{
dt.Columns.Add(new DataColumn(p.Name, p.PropertyType));
}
foreach (T item in GenList)
{
T[] row = new T[pi.Length];
object objthinn = item;
int i = 0;
foreach (PropertyInfo p in pi)
{
//row[i] = (T)item[i];
i++;
}
}
return dt;
}
}
Fig000
On Nov 13, 5:16 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
On 2007-11-13 13:46:33 -0800,fig000<neilnewton...@yahoo.comsaid:
I probablly commented the code out by accident in trying different
things. Here is the article I got this from.
http://codebetter.com/blogs/brendan....06/05/11/14453...
I'm trying to be able to pass any kind of business object collection
and turn it into a datatable to pass back to a objectdatasource which
in turn will use it for a gridview or formview. If you look at the
article you see that I'm trying to be type free in my creation of this
datatable. It seemed to be suggested by the article.
Sorry, I still don't understand. The article you referenced already
doesn't care about the type. It's pretty close to being "type free"
already. What doesn't it accomplish that you want?
In the example code from that article, as long as it supports the IList
interface, you can use any data type to populate a DataTable as shown
in the article. Actually, as near as I can tell, the code in that
article doesn't use anything in IList except the fact that it's
enumerable, so you could actually use IEnumerable as the parameter type
instead.
As far as being "type free" goes, IMHO that's sort of the opposite of
generics. A generic is not really "type free" so much as it allows for
easy creation of multiple concretely typed objects. When used, a
concrete generic instance is actually completely statically typed. I
wouldn't call that "type free". Even though the code in the generic
would not depend on a specific type, the result of using a generic is
very much typed.
Maybe I'm just misunderstanding what you mean by "type free", but I
admit...I still don't get what you're trying to do.
Pete