Question
· Mar 23, 2017

Converting objects between classes

Let's say I have two classes: A and B. They share no common parent except %SystemBase. But these classes have some properties with the same name (type may be the same or not):

Class A {

Property data As C;

Property data2;

}
Class B {

Property data As D;

Property data3;

}

I need to automatically convert  object of class A into object of class B. I'm planing to write a class generator that generates a separate class that converts A to B.  It would work like this:

1. Query %Dictionary to get list of properties:

  • With the same names
  • With both properties being of datatype or object type

2. Iterate over result and generate convert method:

  • If both properties are datatypes, generate simple set line
  • If both properties are objects
    • Check if class generator already generated converter class for this pair of classes, call class generator if not
    • Add a call to the converter class

Is it a viable approach?

Discussion (4)0
Log in or sign up to continue