Is there a trick in creating a generic list of anonymous type?

前端 未结 9 1715
有刺的猬
有刺的猬 2021-01-30 05:25

Sometimes i need to use a Tuple, for example i have list of tanks and their target tanks (they chase after them or something like that ) :

List

        
9条回答
  •  情话喂你
    2021-01-30 05:57

    How about ExpandoObject ?

    dynamic tuple = new ExpandoObject(); 
    tuple.WhatEverYouWantTypeOfTank = new Tank(); // Value of any type
    

    EDITS:

    dynamic tuple = new ExpandoObject();
    tuple.AttackingTank = new Tank();
    tuple.TargetTank = new Tank();
    
    var mylist = new List { tuple };
    
    //access to items
    Console.WriteLine(mylist[0].AttackingTank);
    

提交回复
热议问题