-1

I have a RESTful service which returns JSON that I am deserialising into classes in c#. I need to map some of the properties from the deserialised object model into properties in a different class.

However, I would like to do this through an (xml?) config file which can specify the from/to property names, so that mappings can be changed without recompiling code.

For example:

objectA.Name.FirstName = objectB.FirstName

objectA.Name.LastName = objectB.LastName

What is the best way to do this?

Popo
  • 2,402
  • 5
  • 33
  • 55

2 Answers2

1

You could let something like AutoMapper do the mapping for you.

There are samples in the source code and configuration options in the wiki.

si618
  • 16,580
  • 12
  • 67
  • 84
  • If you are not too worried about performance AutoMapper will work just fine, otherwise manual mapping is the moest performant. – shenku Feb 17 '14 at 23:23
0

If you want it to to be based on late binding you can use reflection to dynamically execute the property assignments based on xml definitions.

You can see some examples in this asnwer: Set object property using reflection

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135