Question

I have a few classes delivered by someone else that are used for serialization. Those classes are constructed with a number of parameters that have a default value. I need to have them initiated with othder default values. It is implemented like this:

public class Header
{
    public class SubHeader1
    {
        [XmlElement("paramone")]
        public string param1{get;set;}
        [XmlElement("paramtwo")]
        public string param2{get;set;}
        [XmlElement("paramthree")]
        public string param3{get;set;}
        [XmlElement("paramfour")]
        public string param4{get;set;}
        public SubHeader1()
        {
            param1 = "theirdefault";
            param2 = "theirdefault";
            param3 = "theirdefault";
            param4 = "theirdefault";
        }
    }
    public class SubHeader2
    {
        [XmlElement("paramone")]
        public string param1{get;set;}
        [XmlElement("paramtwo")]
        public string param2{get;set;}
        public SubHeader1()
        {
            param1 = "theirdefault";
            param2 = "theirdefault";
        }
    }
    Public class SubHeader3
    {
        public class SubSubHeader
        {
            [XmlElement("paramone")]
            public string param1{get;set;}
            [XmlElement("paramtwo")]
            public string param2{get;set;}
            [XmlElement("paramthree")]
            public string param3{get;set;}
            [XmlElement("paramfour")]
            public string param4{get;set;}
            ...
            [XmlElement("paramtwentyfive")]
            pulic string param25{get;set;}
            public SubHeader1()
            {
                param1 = "theirdefault";
                param2 = "theirdefault";
                param3 = "theirdefault";
                param4 = "theirdefault";
                ...
                param25 = "theirdefault";
            }
        }
        [XmlElement("paramone")]
        public string param1{get;set;}
        [XmlElement("subsubheader")]
        public SubSubHeader SSubHeader;
        public SubHeader3()
        {
            SSubHeader = new SubSubHeader();
            param1 = "theirdefault";
        }
    }
    [XmlElement("subheaderone")]
    public SubHeader1 SubHeaderOne;
    [XmlElement("subheadertwo")]
    public SubHeader2 SubHeaderTwo;
    [XmlElement("subheaderthree")]
    public SubHeader3 SubHeaderThree;
    public Header()
    {
        SubHeaderOne = new SubHeader1();
        SubHeaderTwo = new SubHeader2();
        SubHeaderThree = new SubHeader3();
    }
}

That is what is made standard for each class that inherits. I have some other info that needs to be initiated, mainly in the subsubheader.

I created my own SubHeader3 object and created a constructor in their class (for testing purposes) that takes this object. I tried to update the default values with mine via generics like this

public static void MergeWith<T>(this T primary, T secondary) {
    foreach (var pi in typeof(T).GetProperties()) {
       var priValue = pi.GetGetMethod().Invoke(primary, null);
       var secValue = pi.GetGetMethod().Invoke(secondary, null);
       if (priValue == null || (pi.PropertyType.IsValueType && priValue == Activator.CreateInstance(pi.PropertyType))) {
          pi.GetSetMethod().Invoke(primary, new object[]{secValue});
       }
    }
}

But this only works for values in the SubHeader3 class, not the nested SubSubClass. Is this the best way to to this, and how does one get it to cover the subclasses wile being compiler safe. Or am I approaching this the wrong way?

Was it helpful?

Solution

Try merging with Automapper.

 a.InjectFrom(b)
  .InjectFrom(c)
  .InjectFrom<SomeOtherMappingAlgorithmDefinedByYou>(dOrBOrWhateverObject);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top