Getting and Setting Dynamic Business Object Properties

Getting and Setting Dynamic Business Object Properties

Old forum URL: forums.lhotka.net/forums/t/11087.aspx


CrikeyMoses posted on Wednesday, January 25, 2012

We are attempting to do an import from a file (Excel in my case).
I can parse the file fine and get the column headings and values fine, the issue is setting these values to the correct properties of the BO. Its hard to explain, maybe the following example of what we are trying to do might help.

So i loop through the  DictionaryEntry key\value pair i get back from the excel file, and want to assign this value to a property on my BO.

Participant newPart = new Participant();
for (int currentRow = 1; currentRow < datasource.Count(); currentRow++)
{
    foreach (DictionaryEntry pair in datasource.ElementAt(currentRow))
    {
          string col = colums[pair.Key].ToString();          // The Column or Properties
          string value = pair.Value.ToString();                  // The Value to fill that Property
          // Then using reflection, i get all the properties of the BO
          foreach (System.Reflection.PropertyInfo property in typeof(Participant).GetProperties())
          {
               if (property.Name == col)
               {
                     // IN HERE I WANT TO SET THE VALUE TO THE PROPERTY
                     // I JUST DONT KNOW HOW TO GET THAT PROPERTY 
                     newPart.ParticipantId = value;  // WHERE ParticipantId would be col
               }

           }
      }
}

If anyone understands what im trying to do here and can help it would be great!
Also let me know if you need any further info. 

skagen00 replied on Wednesday, January 25, 2012

Don't you just want property.SetValue(newPart, value, null); ?  (I think the third prop is an index for indexed properties if I remember right)

i.e. use the propertyinfo that you have.

JonnyBee replied on Wednesday, January 25, 2012

The simplest answer is to use MethodCaller (a hidden gem for reflection in Csla.Reflection namespace:

      for (int currentRow = 1; currentRow < datasource.Count(); currentRow++)
      {
        Participant newPart = new Participant();
        foreach (DictionaryEntry pair in datasource.ElementAt(currentRow))
        {
          string col = colums[pair.Key].ToString();          // The Column or Properties
          string value = pair.Value.ToString();                  // The Value to fill that Property
          // Then using reflection, i get all the properties of the BO
          MethodCaller.CallPropertySetter(newPart, col, value);
        }
      }
or if you are using managed properties and code is inside the BO:
    public void FillValues(columnDictionary columns, datasourceRow element)
    {
      foreach (DictionaryEntry pair in element)
      {
        string col = colums[pair.Key].ToString();          // The Column or Properties
        string value = pair.Value.ToString();              // The Value to fill that Property
 
        var fieldInfo = FieldManager.GetRegisteredProperties().Where(p => p.Name == col).First();
        LoadProperty(fieldInfo, value);
      }
    }

RockfordLhotka replied on Wednesday, January 25, 2012

You can also get the list of properties from the field manager. That will give you a collection of IPropertyInfo objects that include a property name property. Then you could use the non-generic LoadProperty method to load the properties.

The result should be very similar to what you'd do to load the object from a normal database operation.

Copyright (c) Marimer LLC