Hello,
I am new to this forum and hope that I stick to the guideline.
Working with Nhibernate was fun and painfree as well.
Now I am trying to get more into detail.
I want to write a 'generic' function(using System.Reflection) that allows my application to populate an object into a datatable/datagrid. This is working fine already, until it comes to objects (FKs for example).
I would be happy If I could get the 'Value' out of the Object, or a list that I later could put into a dropDownList.
I hope I that could make myself clear?
any ideas suggestions
thx
that's what I got so far( working for all simple datatypes)
Code:
public static void ObjectToTable(Object p_obj,ref DataSet p_ds, String p_tableName)
{
int count=0;
Type t = p_obj.GetType();
PropertyInfo[] tmpP = t.GetProperties();
if (p_ds.Tables[p_tableName] == null)
{
p_ds.Tables.Add(p_tableName);
foreach (PropertyInfo xtemp2 in tmpP)
{
p_ds.Tables[p_tableName].Columns.Add(xtemp2.Name);
}
}
Object[] tmpObj = new Object[tmpP.Length];
for (Int32 i=0;i<=tmpObj.Length-1;i++)
{
tmpObj[i] = t.InvokeMember(tmpP[i].Name ,
BindingFlags.GetProperty, null,
p_obj, new object[0]);
if (tmpObj[i] == "namespace.Domain.myrelation")
{
// do somethink funky to get it working
}
}
p_ds.Tables[p_tableName].LoadDataRow(tmpObj,true);
}
and back for those who are interested
Code:
public static void TableToObject(DataColumnCollection p_dcc,DataRow p_dr, Object p_object)
{
Type t = p_object.GetType();
for (Int32 i=0;i<=p_dcc.Count -1;i++)
{
try
{
t.InvokeMember(p_dcc[i].ColumnName ,
BindingFlags.SetProperty , null,
p_object,
new object[] {p_dr[p_dcc[i].ColumnName]});
}
catch (Exception ex)
{
if (ex.ToString() != null)
{}
}
}