Expando object - adding dynamic properties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mattb123
    New Member
    • May 2010
    • 8

    Expando object - adding dynamic properties

    Not sure if this is possible or not but I'd like to have an ExpandoObject (.Net 4.0) that I can add properties to without knowing the property name until runtime. I have tried a couple of things but have had no luck yet. Can I do this? Here's a failed attempt:

    Code:
                dynamic _Item = new ExpandoObject();
    
                foreach (DataRow r in dtIT.Rows)
                {
    
                        //assign any matching column names to object properties
                    foreach (DataColumn c in dtIT.Columns)
                    {
                        {
                                _Item.c.ColumnName = r[c.ColumnName];
    ...
    Any suggestions? Thanks!
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    It looks like you made a new _item but never made a new .c inside that _item.

    So when you try to assign something to _item.c.columnn ame - it doesn't exist.

    try this:

    Code:
    dynamic _Item = new ExpandoObject();
    _Item.c = new DataColumn();
    Now you should have a DataColumn inside your _Item object that you can assign to.

    Other examples I've seen just make a new ExpandoObject inside the parent ExpandoObject

    Code:
    dynamic _Item = new ExpandoObject();
    _Item.c = new ExpanoObject();
    _Item.c.Name = "Bob";
    Etc.

    Comment

    • mattb123
      New Member
      • May 2010
      • 8

      #3
      Originally posted by tlhintoq
      It looks like you made a new _item but never made a new .c inside that _item.

      So when you try to assign something to _item.c.columnn ame - it doesn't exist.

      try this:

      Code:
      dynamic _Item = new ExpandoObject();
      _Item.c = new DataColumn();
      Now you should have a DataColumn inside your _Item object that you can assign to.

      Other examples I've seen just make a new ExpandoObject inside the parent ExpandoObject

      Code:
      dynamic _Item = new ExpandoObject();
      _Item.c = new ExpanoObject();
      _Item.c.Name = "Bob";
      Etc.
      Thanks, but that isn't exactly what I'm trying to do.
      For example, there is a DataColumn called descrip of type string in the DataTable. I want to create _Item.descrip and populate with the text value of the current r.descrip without explicitly declaring _Item.descrip. Can I do that?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        You have to make a thing, before you can assign value to a thing. ExpandoObjects aren't magical, just easier.

        But you can make a thing at run-time, then give it value. You don't have to pre-define it in code at design-time. Isn't that what you are trying to accomplish?

        Code:
        dynamic _Item = new ExpandoObject();
         
                    foreach (DataRow r in dtIT.Rows)
                    {
         
                            //assign any matching column names to object properties
                        foreach (DataColumn c in dtIT.Columns)
                        {
                            {
        // add this line
        _Item.c = new DataColumn();
        // Now you have a thing you can give value to
                                    _Item.c.ColumnName = r[c.ColumnName];

        Comment

        • mattb123
          New Member
          • May 2010
          • 8

          #5
          Right. I'm just trying to see if I can dynamically create the properties, so in the end I have one expandoobject per row in this table with one property for each field in that row with the same name as the column populated with the value in the datarow.
          So in the end instead of _Item.c.descrip , I want to have _Item.descrip and the value of item.descrip would equal the value of r["descrip"]. Then in the future if the descrip column name changes to "descriptio n", I'd still like it to work without having to modify it. So if the column name changed then _Item.descripti on = r["descriptio n"].
          Sorry if I'm not being clear (or asking the impossible - I'd accept that answer if that was the case).

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Matt: o in the end instead of _Item.c.descrip , I want to have _Item.descrip
            Then stop making an _Item.c. layer. If you only want _Item.descrip then only make _Item.descrip Its your code that is generating that hierarchy.
            Code:
            _Item.c.ColumnName = r[c.ColumnName];
            Just because you have to reference the DataColumn C on the right side of the equasion doesn't force you to use it on the left.

            If you want to do this, go right ahead

            Code:
            _Item.descrip = r.Columns[2];]
            Now the name doesn't matter, so long as the third column is the description.

            But if you are trying to somehow magically map an unknown and changing number of columns with unknown and changing names, to known properties... I don't see a way you can always be certain. *Some* rules have to govern any system. You have to know either the names or their positions to be sure you are getting what you expect to get.

            You could search for a column whose name starts with "Descrip", and match to anything from 'Descript" to "Descriptio n" so long as the start is right.

            Comment

            • mattb123
              New Member
              • May 2010
              • 8

              #7
              Originally posted by tlhintoq
              Then stop making an _Item.c. layer. If you only want _Item.descrip then only make _Item.descrip Its your code that is generating that hierarchy.
              Code:
              _Item.c.ColumnName = r[c.ColumnName];
              Just because you have to reference the DataColumn C on the right side of the equasion doesn't force you to use it on the left.

              If you want to do this, go right ahead

              Code:
              _Item.descrip = r.Columns[2];]
              Now the name doesn't matter, so long as the third column is the description.

              But if you are trying to somehow magically map an unknown and changing number of columns with unknown and changing names, to known properties... I don't see a way you can always be certain. *Some* rules have to govern any system. You have to know either the names or their positions to be sure you are getting what you expect to get.

              You could search for a column whose name starts with "Descrip", and match to anything from 'Descript" to "Descriptio n" so long as the start is right.
              Thanks again!

              Comment

              Working...