0

Following code gives a red squigly under "emp.(col.ColummnName)". Error is "identifier expected"

foreach (DataColumn col in dt.Columns)
{
   emp.(col.ColumnName) = row[(col.ColumnName)];
}

emp is a custom class with property names which correspond to column names in dataTable dt.

I suspect that I have to construct the expression differently so that I can refer to a property of class emp with results of a method call (col.ColumnName).

Any ideas will be appreciated.

==========================

Final Answer with working function code;

public void rowToObject(ref DataRow dr, ref object myObj)
{
    foreach (DataColumn dc in dr.Table.Columns)
    {
        string colName = dc.ColumnName;
        object colValue = dr[colName];

        if (object.ReferenceEquals(colValue, DBNull.Value))
        {
            colValue = null;
        }

        PropertyInfo pi = myObj.GetType().GetProperty(colName);
        if (pi != null && colValue != null)
        {
            Type propType = null;
            Type nullableType = Nullable.GetUnderlyingType(pi.PropertyType);

            if (nullableType != null)
            {
                propType = nullableType;
            }
            else
            {
                propType = pi.PropertyType;
            }

            if (object.ReferenceEquals(propType, colValue.GetType()))
            {
                pi.SetValue(myObj, colValue, null);
            }
        }
    }
}
Salman Siddiqui
  • 340
  • 3
  • 13

3 Answers3

6

This syntax is not supported, the property name must be known at compile time. You can use reflection instead:

foreach (DataColumn col in dt.Columns)
{
   PropertyInfo prop = emp.GetType().GetProperty(col.ColumnName);
   prop.SetValue(emp, row[col.ColumnName], null);
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 1
    Just be careful that the data type returned from row[col.ColumnName] is compatible with the type of the property you are setting. – apiguy Nov 14 '11 at 21:16
  • Can the SetValue(emp, row[col.ColumnName], null) be cast to the same datatype as prop in-line? – Salman Siddiqui Nov 14 '11 at 21:19
  • To Cast a property value dynamically use the function code located at http://stackoverflow.com/questions/907882/cast-a-property-to-its-actual-type-dynamically-using-reflection/909666#909666 – Salman Siddiqui Nov 14 '11 at 21:28
  • Theoretically, Convert.ChangeType(row[col.ColumnName], prop.PropertyType) should do the trick. Am going to edit the code in answer to reflect this :) – Salman Siddiqui Nov 14 '11 at 21:59
  • @udp1024, Convert.ChangeType will probably work fine in *most* cases, but keep in mind it doesn't work in *all* cases – Thomas Levesque Nov 14 '11 at 22:13
1

This dynamic access of class properties is really beneficial, but it's not as easy to implement as you are hoping for.

You need to look into Reflection... with this library, you can dynamically make and fill your classes, but it takes some reading and trial/error.

Here is a couple links that discuss your situation; however, you may want to look up a Reflection 101 tuturial first.:

.

.

Please vote if helpful

Community
  • 1
  • 1
Ray K
  • 1,452
  • 10
  • 17
0

I'm not aware that you can do it in way that you're trying to do it. What you're looking for is reflection and you can find an example how it works in this post:

How to loop through all the properties of a class?

Community
  • 1
  • 1
Marko
  • 12,543
  • 10
  • 48
  • 58