-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: nhibernate with reflections
PostPosted: Wed Jul 26, 2006 11:58 pm 
Newbie

Joined: Wed Jul 26, 2006 11:39 pm
Posts: 1
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)
               {}
            }
         }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 6:26 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
it is way too complex and slow (and useless in my opinion)! it is simpler to override the ToString() method for your objects as the UI interface controls call this method when they encounter any object (even primitives)

cheers,
radu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.