2016-04-06 27 views
0

Merhaba Diğer karmaşık türlerin içinde bulunan karmaşık türlerin genişletilebilir bir koleksiyonuna sahip olmak istiyorum. ben bunu yapmak istiyorum nasılHerhangi bir koleksiyondaki karmaşık türlerin içerdiği karmaşık karmaşık türler (Özellik ızgarası)

private static void SetExpandableAttrForType(Type type) 
    { 
     var props = type.GetProperties(); 
     foreach (var prop in props.Where(x =>!x.PropertyType.IsSimpleType()&& x.CanWrite)) 
     { 
      SetExpandableAttrForType(prop.PropertyType); 
     } 
     TypeDescriptor.AddAttributes(type, new TypeConverterAttribute(typeof (ExpandableObjectConverter))); 
    } 

ve sonra

SetExpandableAttrForType(arrayInstance.GetType().GetElementType()); 

Testi modeli:

public class Class1 
{ 
    public Class2 Class2Inclass1 { get; set; } 
    public Class2[] Class2Array { get; set; } 
} 


public class Class2 
{ 
    public Class3 Class3Inclass2 { get; set; } 
    public string Class2String { get; set; } 
    public string Class2String2 { get; set; } 
} 


public class Class3 
{ 
    public Class4 Class4Inclass3 { get; set; } 
    public string Class3String { get; set; } 
    public int Class3Int { get; set; } 
} 

public class Class4 
{ 
    public int Class4Int { get; set; } 
    public DateTime Class4Datetime { get; set; } 
} 

Bu türleri için ince fakat türlerinin toplanması için çalışır.enter image description here

cevap

0

Programlama ile ilgili en önemli şey, birilerinin sorun hakkında konuştuğunda, genellikle başka bir açıdan bakmaya başlarsınız. Sorun, bu yuvalanmış karmaşık türün örneğine ihtiyacım vardı. CellValueChanged olayı bana tip verir ve sonra sadece bunun örneğini oluşturmam gerekir.

private void propertyGridControl1_CellValueChanged(object sender, CellValueChangedEventArgs e) 
    { 
     var changedObject = e.Value; 
     if (changedObject != null) 
     { 
      if (changedObject.GetType().IsArray || changedObject.GetType().IsGenericList()) 
      { 
       var collectionItems = changedObject as IEnumerable; 
       if (collectionItems != null) 
        foreach (var item in collectionItems) 
        { 
         SetValueOfCollectionComplexObject(item); 
        } 
      } 
     } 
    } 


public void SetValueOfCollectionComplexObject(object item) 
{ 
     var complexProps = item.GetType().GetProperties().Where(x => !x.PropertyType.IsSimpleType()); 
     foreach (var prop in complexProps) 
     { 
      if (prop.GetValue(item) == null) 
      { 
       prop.SetValue(item, Activator.CreateInstance(prop.PropertyType)); 
       SetValueOfCollectionComplexObject(prop.GetValue(item)); 
      } 
     } 
    }