Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 任何集合中复杂类型中的可消耗复杂类型(属性网格)_C#_Devexpress - Fatal编程技术网

C# 任何集合中复杂类型中的可消耗复杂类型(属性网格)

C# 任何集合中复杂类型中的可消耗复杂类型(属性网格),c#,devexpress,C#,Devexpress,你好,我想要一个复杂类型的可扩展集合,它位于其他复杂类型的内部。 我想如何做到这一点: private static void SetExpandableAttrForType(Type type) { var props = type.GetProperties(); foreach (var prop in props.Where(x =>!x.PropertyType.IsSimpleType()&& x.CanWrite))

你好,我想要一个复杂类型的可扩展集合,它位于其他复杂类型的内部。 我想如何做到这一点:

 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)));
    }
然后

  SetExpandableAttrForType(arrayInstance.GetType().GetElementType());
测试模型:

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; }
}
它适用于类型,但不适用于类型集合。
编程的好处在于,当你告诉别人这个问题时,通常你会从另一个角度来看待它。问题是我需要这个嵌套复杂类型的实例。CellValueChanged事件给我类型,然后我只需要创建它的实例

   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));
            }
        }
    }