6

为什么SomeClass.ClassField.StructField属性在 a 中没有变化propertyGrid?看来,实例更改后propertyGrid不会调用。但是相同的代码可以很好地与而不是.SomeClass.ClassField.setSomeStructPointSomeStruct

[TypeConverter(typeof(ExpandableObjectConverter))]
public struct SomeStruct
{
    private int structField;

    public int StructField
    {
        get
        {
            return structField;
        }
        set
        {
            structField = value;
        }
    }

    public override string ToString()
    {
        return "StructField: " + StructField;
    }
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public sealed class SomeClass
{
    public SomeStruct ClassField
    {
        get;
        set;
    }
}

...

var someClass = new SomeClass
{
    ClassField = new SomeStruct
    {
        StructField = 42
    }
};
propertyGrid.SelectedObject = someClass;
4

3 回答 3

8

您需要一个特殊的 TypeConverter 来覆盖TypeConverter.GetCreateInstanceSupported因为否则按值复制/装箱魔术会在属性网格处理所有这一切的方式背后发生。

这是一种适用于大多数值类型的方法。你这样声明它:

[TypeConverter(typeof(ValueTypeTypeConverter<SomeStruct>))]
public struct SomeStruct
{
    public int StructField { get; set; }
}


public class ValueTypeTypeConverter<T> : ExpandableObjectConverter where T : struct
{
    public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
    {
        if (propertyValues == null)
            throw new ArgumentNullException("propertyValues");

        T ret = default(T);
        object boxed = ret;
        foreach (DictionaryEntry entry in propertyValues)
        {
            PropertyInfo pi = ret.GetType().GetProperty(entry.Key.ToString());
            if (pi != null && pi.CanWrite)
            {
                pi.SetValue(boxed, Convert.ChangeType(entry.Value, pi.PropertyType), null);
            }
        }
        return (T)boxed;
    }
}

请注意,它不支持纯字段结构,仅支持具有属性的结构,但 ExpandableObjectConverter 也不支持这些,它需要更多代码才能做到这一点。

于 2013-04-02T07:36:59.533 回答
3

我调整了 Simon Mourier 的答案,以避免 ValueTypeTypeConverter 成为泛型的需要:

public class ValueTypeTypeConverter : System.ComponentModel.ExpandableObjectConverter
{
    public override bool GetCreateInstanceSupported(System.ComponentModel.ITypeDescriptorContext context)
    {
        return true;
    }

    public override object CreateInstance(System.ComponentModel.ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
    {
        if (propertyValues == null)
            throw new ArgumentNullException("propertyValues");

        object boxed = Activator.CreateInstance(context.PropertyDescriptor.PropertyType);
        foreach (System.Collections.DictionaryEntry entry in propertyValues)
        {
            System.Reflection.PropertyInfo pi = context.PropertyDescriptor.PropertyType.GetProperty(entry.Key.ToString());
            if ((pi != null) && (pi.CanWrite))
            {
                pi.SetValue(boxed, Convert.ChangeType(entry.Value, pi.PropertyType), null);
            }
        }
        return boxed;
    }
}
于 2014-01-03T02:09:52.410 回答
0

就我而言,通用参数在编译时是未知的(插件的选项结构)。您可以使用以下方法获取当前值的副本context.PropertyDescriptor.GetValue(context.Instance);

  public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
  {
     if (propertyValues == null)
        throw new ArgumentNullException("propertyValues");

     object boxed = context.PropertyDescriptor.GetValue(context.Instance);
     foreach (DictionaryEntry entry in propertyValues)
     {
        PropertyInfo pi = boxed.GetType().GetProperty(entry.Key.ToString());
        if (pi != null && pi.CanWrite)
           pi.SetValue(boxed, Convert.ChangeType(entry.Value, pi.PropertyType), null);
     }
     return boxed;
  }
于 2015-06-22T12:03:09.047 回答