反序列化自定义组件后如何执行代码?

how to execute code after deserializing a custom component?

我正在尝试实现一种在 deserializing 上调用的方法 custom component
但是,标记为 OnDeserializingOnDeserialized 的方法永远不会被调用。

我在 SO 上发现了 this 问题,从文本中我得出结论,这里正在调用这些方法。所以我将这段代码与我的进行了比较。
同样在 documentation 中,我看不到任何我遗漏的东西。

我需要的是,当我的 custom component 在设计时从 Designer.cs 变成 deserializing 时,我可以介入并做一些额外的编码。

那么我在这里错过了什么?

[Serializable]
public partial class gttDataTable : Component, ISerializable
{
    private Collection<ConfigColumn> _columns = new Collection<ConfigColumn>();

    public gttDataTable()
    { }

    public gttDataTable(SerializationInfo info, StreamingContext context)
    { }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Collection<ConfigColumn> gttColumns
    {
        get { return _columns; }
        set { _columns = value; }
    }

    [OnDeserializing]
    internal void OnDeserializingMethod(StreamingContext context)
    {
        // this code is never called
        throw new NotImplementedException();
    }

    [OnDeserialized]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        // this code is never called
        throw new NotImplementedException();
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        // this code is never called
        throw new NotImplementedException();
    }
    
    private IComponentChangeService GetChangeService()
    {
        return (IComponentChangeService)GetService(typeof(IComponentChangeService));
    }
}

public class ConfigColumn
{
    public string Name { get; set; }
    public string Caption { get; set; }
    public string ColumnName { get; set; }
    public Type DataType { get; set; }
}

编辑
为清楚起见,问题是当自定义组件反序列化时,两个内部方法都不会被调用。

编辑 2
我尝试按照建议制作内部方法 public,但没有任何区别。他们仍然没有被调用

编辑 3
我阅读了 this link 并仔细检查了所有内容是否与文档中的内容相同。在我看来都是正确的,但仍然没有调用方法

并非所有序列化程序都考虑 OnDeserializing 等属性。请参见例如Why does the OnDeserialization not fire for XML Deserialization?.
如果我没记错的话设计师确实使用 CodeDomSerializer.

要进行自定义序列化,您需要从 CodeDomSerializer 派生并用 DesignerSerializerAttribute

装饰 class
[DesignerSerializerAttribute(typeof(YourCustomSerializer), typeof(CodeDomSerializer))]
public partial class gttDataTable : Component
{
}