无法让我的自定义控件从设计器序列化

问题描述

我正在构建一个 custom component,它有一个 DataTable 作为属性之一。
一切都运行良好,除了我无法将 designerserialize DataTable 属性中的列。

我尝试了 solution in this answer 但它没有解决我的问题。

换句话说,当我把这个自定义组件放到设计器的一个表单上时,然后执行以下步骤

  • 填写数据库属性
  • 填写 SelectText 属性
  • 属性 DoRefreshSchema 选择值 true(它将在设计器中保持为 false,因为它仅用于触发代码以填充 DataTable 的列)

然后在设计器的属性窗口中,我可以看到DataTable的所有列都按预期填充。

enter image description here

但是在构建或关闭/打开表单后,它们又都消失了......

那么我在这里做错了什么。

这是来自自定义控件的代码(已剥离以仅显示相关代码

[Serializable]
public partial class gttDataTable : Component,ISerializable
{
    private DataTable _Table = new DataTable();
    private string _SelectText = "";
    private bool _DoRefreshSchema = false;

    public gttDataTable()
    { }

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]        
public DataTable Table
{
    get { return _Table; }
    set { _Table = value; }
}

[Editor("System.ComponentModel.Design.MultilinestringEditor,System.Design,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a",typeof(UITypeEditor))]
public string SelectText
{
    get { return _SelectText; }
    set { _SelectText = value; }
}

public gttDataBase DataBase
{
    get { return _gttDataBase; }
    set { _gttDataBase = value; }
}

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool DoRefreshSchema
{
    get { return _DoRefreshSchema; }
    set
    {
        _DoRefreshSchema = false;
        if (value)
        {
            RefreshSchema();
            // After this the DataTable has all columns,they are all present in the proporty window of the designer.
            // So I hoped that this code below would tell the designer to serialize the DataTable,but it does not
            this.GetChangeService().OnComponentChanged(this,null,null);
        }
    }
}


public void RefreshSchema()
{
    if (DataBase != null && SelectText != "")
    {
        using (DataTable tempTable = new DataTable())
        {
            // THE Code below fetches the table's schema from the database and returns them as columns in `tempTable` 
            DataBase.FillDataTable(tempTable,Concat("SET FMTONLY ON",Environment.NewLine,SelectText,Environment.NewLine + "SET FMTONLY OFF"));
        }

        // This loop then create's a column in the DataTable property for
        // every column find in tempTable
        foreach (DataColumn col in tempTable.Columns)
        {
            // I would really like these columns to get serialized in 
            // designer.cs
            _Table.Columns.Add(col.ColumnName,col.DataType);
        }
    }
}


// I added own serialization code,because I hoped that this would solve my issue,but it does not

private IComponentChangeService GetChangeService()
{
    return (IComponentChangeService)GetService(typeof(IComponentChangeService));
}

// Implement this method to serialize data. The method is called
// on serialization.
public void GetobjectData(SerializationInfo info,StreamingContext context)
{
    // Use the AddValue method to specify serialized values.
    info.AddValue("DataBase",DataBase,typeof(gttDataBase));
    info.AddValue("SelectText",typeof(string));
    info.AddValue("Table",Table,typeof(DataTable));
}
    
    
// The special constructor is used to deserialize values.
public gttDataTable(SerializationInfo info,StreamingContext context)
{
    // Reset the property value using the GetValue method.
    DataBase = (gttDataBase)info.GetValue("DataBase",typeof(gttDataBase));
    SelectText = (string)info.GetValue("SelectText",typeof(string));
    Table = (DataTable)info.GetValue("Table",typeof(DataTable));
}

编辑
我尝试添加

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

按照评论中的建议添加到 DataTable 属性,但没有帮助

编辑 2

如果我手动将一列添加属性 Table 中,然后更改它的属性中的某些内容,则该列将被序列化并且是持久的。
但是代码添加的所有其他列都不是。
有没有办法也序列化它们?

编辑 3

为了清楚起见,我们的想法是在设计时将 DataTable 属性的列序列化到 Designer.cs 文件中。
不是在运行时

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)