How do I build a single function with which I can convert a nested List to a nested object[] array effectively?
ps the depth of the nested List varies.
for example:
public class ProductBindingViewSC
{
public int ProductID { get; set; }
public string aName { get; set; }
public List<Product_SuperType_Info> SuperTypeList { get; set; }
public List<Product_Image_Info> Product_ImageList { get; set; }
public List<Product_Property_BindingViewSC> Property1 { get; set; }
}
Property1 has structure like ProductBindingViewSC ( it has fields and List)
I need to do the following with only a few lines
List<ProductBindingViewSC> productList = new GetProductObject().getProductBySomeCondition( 123 );
var productArray = SomeFunction(productList );
or
ProductBindingViewSC product = new GetProductObject().getProductById( 345 );
var product = SomeFunction(product );
ProductArray has the exact same structure as ProductBindingViewSC but all the List Type are Array Type instead. And if one of the object in List is empty, the array class will have one record of empty object in the Array .
EDIT: Question : how should I write the SomeFunction . I can only think of looping with very ineffecient way and mine is not generic. I need a function which I can throw in any object type.
Personnally, I'd just manually write a function that converts a ProductBindingViewSC into a ProductArray member by member, and then use List.ToArray()+Array.ConvertAll() or Linq's .Select().ToArray().
You could also use reflection, even make that not too slow thanks to open property delegates and generic type instantiation (in short, anything to avoid a sluggish GetValue()), to automatically convert from one type to the other, but that's quite a long story to explain.
ICopyMember<TIn, TOut>
to expose a copy method but not the type of the members. Then implement this interface in a generic class PropertyCopier<Tin, TOut, TPropIn, TPropOut>
that copies a member using open property delegates and probably later a recursive call to the mass-copy function. TIn
and TOut
, you should obtain a collection of ICopyMember<TIn, TOut>
, one for each property. Type
of the source and destination objects as key (or just that of the source object, if it's always copied to the same destination class). productList.ToArray();
顺便说一句,List有什么问题?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.