Question

Note: ALL code is hand written so syntax might be wrong I don't know

I want to merge two objects of a partial class generated by XSD2Code tool but not able to find out how.

I find this post which doesn't help either How to combine a Partial Class Object in C#? as Partial class I have has like hundred of properties and attributes. Also this code is copying not merging left.price = right.price;

Example

Public Method_1()
{ 
      FruitCrate fcA = new FruitCrate(); 
      fcA = Method_2() + Method_3(); 

}

Public FruitCrate Method_2()
{ 
FruitCrate fcB = new FruitCrate(); 
fcB.Name = ..
fcB.....  hundred of properties..

return fcB;

}

Public FruitCrate Method_3()
{ 
FruitCrate fcC = new FruitCrate(); 
fcC.Name = ..
fcC.....  hundred of properties..

return fcC;
}

This is how partial class look like,

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.1433")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class FruitCrate{

        private List<FruitCrate> FruitCrate;

        private static System.Xml.Serialization.XmlSerializer serializer;

        public FruitCrate() {
            this.FruitCrateField = new List<FruitCrateField>();
        }

        [System.Xml.Serialization.XmlArrayAttribute(Order=0)]
        [System.Xml.Serialization.XmlArrayItemAttribute("FruitCrate", IsNullable=false)]
        public List<FruitCrate> FruitCrate{
            get {
                return this.FruitCrate;
            }
            set {
                this.FruitCrateField = value;
            }
        }
        //soo on it's a large auto generated class
Was it helpful?

Solution

Why not implement a function to do the addition for you? For the case you mention above with only two cakes, it could look like:

public static FruitCake MergeCakes(FruitCake A, FruitCake B)
{
    FruitCake mergedCake = new FruitCake();

    // Do your merging, like for instance

    mergedCake.Price = A.Price + B.Price;

    return mergedCake;
}

You can then do your addition like this:

  FruitCrate fcA = new FruitCake(); 
  fcA = MergeCakes(Method_2(), Method_3());

If you need the ability to merge a large number of cakes, you can implement your MergeCakes-function with a List input, like for instance:

public static FruitCake MergeCakes(List<FruitCake> cakes)
{
    if(cakes != null)
    {
       FruitCake mergedCake = new FruitCake();

       // Do your merging, like for instance
       foreach(var cake in cakes)
       {
           mergedCake.Price += cake.Price;
       }
    }
    return mergedCake;
}

And then do your addition as follows:

  FruitCrate fcA = new FruitCake(); 
  fcA = MergeCakes(new List<FruitCake>(){ Method_2(), Method_3()), Method_4(), Method_5(), ... });

It might seem like I am not directly answering your question, but in my experience, you are better off keeping things as simple as possible for as long as possible. That way you can look back at your code two weeks from now and still understand whats going on.

Good luck!

OTHER TIPS

Your question has nothing to do with partial classes. A partial class is one whose code is split between multiple files.

There's nothing "built-in" to do what you want. You could use reflection to loop through all of the properties and add any numeric values together, but you would still have to account for different numeric types (there's not a generic way to add two numeric values whose types are not known at compile-time).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top