winforms - c# TypeConverter 和 InstanceDescriptor 的痛苦

标签 winforms serialization user-controls

我做了一个 UserControl 的例子,但仍然(花了一整天)无法解决我的问题。

我希望 UserControl 有一个复杂的属性,以便在设计时查看属性网格内的该属性,并能够更改它等等。 复杂的属性很简单 - 它是一个具有一个字符串属性的类。

现在我有两个问题:

1)当我更改属性网格中的文本值时 - 该值不会转到 Form1.Designer.cs

2)有时,当我重建项目(甚至不需要运行)时,我会弹出一个VS的弹出窗口,其中表示SettingsCoverter无法将Settings转换为InstanceDescriptor。那些设置类是我的。 请帮忙解决这个问题。

 [TypeConverter(typeof(SettingsConverter))]
public class Settings : INotifyPropertyChanged
{
    private string stringText = "123";
    public string StringText
    {
        get { return stringText; }
        set
        {
            stringText = value;
            OnPropertyChanged("StringText");
        }
    }

    public Settings()
    {
    }

    public Settings(string fText)
    {
        StringText = fText;
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

class SettingsConverter : ExpandableObjectConverter
{
    public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
    {
        return new Settings((string)propertyValues["StringText"]);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;
        return base.CanConvertFrom(context, sourceType);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor))
            return true;
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value is Settings)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                Settings settings = (Settings)value;
                object[] properties = new object[1];
                Type[] types = new Type[1];

                types[0] = typeof(string);
                properties[0] = settings.StringText;

                ConstructorInfo ci = typeof(Settings).GetConstructor(types);
                return new InstanceDescriptor(ci, properties);
            }

            if (destinationType == typeof(string))
            {
                Settings settings = (Settings)value;
                return settings.StringText;
            }
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value == null)
            return "";

        if (value is string)
            return new Settings(value as string);

        return base.ConvertFrom(context, culture, value);
    }
}

最佳答案

2)这是建议

http://forums.asp.net/t/1309871.aspx?TypeConverter+Error+InstanceDescriptor+

类型转换器直接进入 CLR 的类型信息缓存并 不经过项目实现的类型解析服务 系统。这意味着 CLR 将从程序集返回类型 之前已在重建之前加载 - 并且转换将失败 因为设计表面上的类型是从新建的 集会。我确认您可以通过确保版本来解决此问题 类库项目的编号在每次构建时自动递增。 您可以通过以下方式执行此操作: 1)调出类库项目的属性 2) 选择“应用程序”选项卡,然后单击“程序集信息...” 按钮。 3) 在版本中,该字段将最后一个条目设置为“*” - 所以应该显示:1 0 0 *

现在,每当构建类库时,修订号(最后一位数字 版本)将自动递增。这会强制 CLR 失效 它已缓存的条目并加载新条目。

关于winforms - c# TypeConverter 和 InstanceDescriptor 的痛苦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15091463/

相关文章:

winforms - 是否可以有一个窗口来插入由 powershell 制作的数据?

Winforms 应用程序模板

c# - 基于快速和脏反射的 XML 序列化程序

interop - 使用 VS.NET 构建 OCX?

WPF 如何让 UserControl 继承 Button?

c# - 在 Windows 窗体中进行验证的最佳方法是什么

c# - 绘制/绘制外部形式

java - Android Studio : SavedInstanceState of object doesn't work

c# - 命名空间更改后的 DataContractSerializer 兼容性

wpf - 内部控件和 View 模型之间的 UserControl 属性 "relay";感觉/工作不正常