2016-05-10 3 views
0

私はクラスのリストを持っています。メンバーを追加したいのですが、これをどうやって達成するのですか?クラス内のクラスのリストを追加するには(可能であればリフレクションが望ましい)

void Main() 
{ 
    var a1 = new A(){id =2}; 
    var a2 = new A(){id =3, xx=1}; 
    var a3 = new A(){id =2, yy=2}; 
    var a4 = new A(){id =2, xx=4, yy=6}; 

    var a5 = new A(){cc = new C(){id =2, xx=1}}; 
    var a6 = new A(){cc = new C(){id =2}}; 
    var a7 = new A(){cc = new C(){id =2}}; 

    var b = new B(); 
    b.SomeMethod(new List<A>{a1, a2, a3, a4, a5, a6, a7}); 
} 

クラスCはクラスAの内部で使用されます。どちらのクラスも3つの空のAPIであるため、変更できません。

public class A 
{ 
     public int id {get;set;} 
     public int xx {get;set;} 
     public int yy {get;set;} 
     public C cc {get; set;} 
} 

public class C 
{ 
      public int id {get;set;} 
      public int xx {get;set;} 
      public int yy {get;set;} 
} 

の代わりに、クラスのリストに、クラスの単一のメンバーを追加し、私はクラスの内部のすべてのメンバーが加算されるクラスのリターンでお願いします。

public class B 
{ 
    public void SomeMethod(IList<A> listOfA) 
    { 
     int result1 = listOfA.Sum(A => A.id);//return 9 
     int result2 = listOfA.Sum(A => A.xx);//return 5 
     int result3 = listOfA.Sum(A => A.yy);//return 8 
     int result4 = listOfA.Where(A=>A.cc != null).Sum(A => A.cc.id); 
//return 6 

     //var resultAll = ???;//return new A(){id = 9, xx = 5, yy = 8, cc = new C(){id =6, xx = 1}} 
    } 
} 

基本的に私はあなたが和を計算する前にnull値をチェックしていることを確認する必要があり

上記
//var resultAll = ???;//return new A(){id = 9, xx = 5, yy = 8, cc = new C(){id =6, xx = 1}} 
+1

、問題文の質問ですが...? http://stackoverflow.com/help/mcve –

+0

申し訳ありませんが、長い勤務時間のため、私は正しい考え方ではありません。私は質問を更新しました、私はそれが少しでも数時間で私の心をクリアするために眠ると思います。もう一度謝ります。 – user3663854

答えて

0

から以下のような結果を達成するためにどのように助けを必要とします。私が正しく理解している場合、コードが

int result4 = listOfA.Where(A=>A.cc != null).Sum(A => A.cc.id); 
int result5 = listOfA.Where(A=>A.cc != null).Sum(A => A.cc.xx); 

var resultAll = new A() { 
         id = result1, 
         xx = result2, 
         yy = result3, 
         cc = new C() 
         { 
          id = result4, 
          xx = result5 
         } 
         }; 
+0

ありがとう!私が直面している2つの問題のうちの1つを助ける、私はあなたの答えで私の質問を編集しましたが、他のものはまだ保留中です... – user3663854

+0

resultAllの値は何でしょうか? – Agalo

+0

はresultAllの答えを更新しました – Agalo

0

を動作するはず続いて、このコードをチェックし、役立つかもしれない:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace CSharpConsoleApplication.Tests 
{ 
    class OperatorsOverloadingTest 
    { 
     public static void Run() 
     { 
      var list = new List<ParentObject>() { 
       new ParentObject() { IntegerValue = 1 }, 
       new ParentObject() { IntegerValue = 2 }, 
       new ParentObject() { Child = new ChildObject() { DoubleValue = 10D } }, 
       new ParentObject() { Child = new ChildObject() { DoubleValue = 20D } } 
      }; 

      ParentObject pTotal = new ParentObject(); 

      foreach (var p in list) 
       pTotal += p; 

      Console.WriteLine(pTotal); 
     } 
    } 

    public class ParentObject 
    { 
     public int IntegerValue { get; set; } 

     public ChildObject Child { get; set; } 



     public ParentObject() 
     { 
      Child = new ChildObject(); 
     } 



     public static ParentObject operator +(ParentObject item1, ParentObject item2) 
     { 
      var parent = new ParentObject() { IntegerValue = item1.IntegerValue + item2.IntegerValue }; 
      parent.Child = item1.Child + item2.Child; 

      return parent; 
     } 



     public override string ToString() 
     { 
      return string.Format("IntegerValue={0}, Child={1}", IntegerValue, Child); 
     } 
    } 



    public class ChildObject 
    { 
     public double DoubleValue { get; set; } 

     public static ChildObject operator +(ChildObject item1, ChildObject item2) 
     { 
      return new ChildObject() { DoubleValue = item1.DoubleValue + item2.DoubleValue }; 
     } 


     public override string ToString() 
     { 
      return string.Format("DoubleValue={0}", DoubleValue); 
     } 
    } 
} 
+0

それは動作しますが、私が言及しなかったのは、親クラスと子クラスの両方が別の3つの空のAPIからのものであり、そこにコンストラクタを置くことができないということです。代替ソリューションを提供することは可能ですか? – user3663854

関連する問題