Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

generics help needed

generics help needed

generics help needed

Hi all,

I am trying to use some code found on cp. The one with the tick :)

This db mapper works but not with nullable types. The method below just loops through datatablerow and fills a class
i am trying to call with this method but how do I pass the T when I call ?


public static T DataTableToObject<T>( DataTable dt, int row) where T : new()
            //string temp = "assignment";
            //string temp1 = temp.Replace("a", "A");
            //List<string> propNames = new List<string>();
            PropertyInfo[] pi = typeof(T).GetProperties();

            T objectName = new T();
            int index = -1;
            for (int i = 0; i < dt.Columns.Count; i++)
                string colName = dt.Columns[i].ColumnName;
                index = GetNamedPropertyIndex(colName, pi);

                if (index != -1)
                    // this is a reference to the class property
                    // so what we do to this will be done to the class
                    PropertyInfo classProperty = pi[index];

                    if (colName.Equals(classProperty.Name, StringComparison.InvariantCultureIgnoreCase))
                        object cellValue = dt.Rows[row][colName];
                        Type cellType = dt.Rows[row][colName].GetType();

                            if (cellValue != DBNull.Value)
                                if (cellValue == DBNull.Value && classProperty.PropertyType.Name == "Int32")
                                    cellValue = -1;
                                else if (cellValue == DBNull.Value && classProperty.PropertyType.Name == "Boolean")
                                    cellValue = false;
                                if (cellValue == DBNull.Value && classProperty.PropertyType.Name == "DateTime")
                                    cellValue = System.DateTime.MinValue;

                                if (cellType.ToString() == "System.Guid")
                                    cellValue = cellValue.ToString();
                                else if (cellType.ToString() == "System.String")
                                    cellValue = @cellValue.ToString();
                                    cellValue = Convert.ChangeType(cellValue, classProperty.PropertyType);
                                classProperty.SetValue(objectName, cellValue, null);
                        catch (Exception ex)
                            throw new Exception("Error in Mapper converting the DT row to an object " +
                                objectName.ToString() + "on property " + classProperty.Name, ex);
            return objectName; 

I want to convert the above method so it will use method below to call setvalue method?


public static T1 To<T1>(object obj)
           where T1 : class
            Type t = typeof(T1);
            Type u = Nullable.GetUnderlyingType(t);

            if (u != null)
                if (obj == null)
                    return default(T1);

                return (T1)Convert.ChangeType(obj, u);
                return (T1)Convert.ChangeType(obj, t);

Age is a consequence of experience

RE: generics help needed

Sorry, not sure if that was clear. I am trying to get the top method to call the bottom method. But how do I pass the Type (eg int?) into the bottom method (T) ? Hope that's a bit clearer.

Age is a consequence of experience

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close