简体   繁体   English

使用客户属性编辑器序列化静态类实例属性

[英]Serialize static class instance property with customer property editor

I've a big partial class with inner partial classes and a non partial class with a lot static members: 我有一个带有内部局部类的很大的局部类,以及一个带有很多静态成员的非局部类:

public partial class kField
{
    public partial class Campaign
    {
        public class Brand
        {
            public static kFieldClass id = new kFieldClass("id", typeof(long));
            public static kFieldClass abc = new kFieldClass("abc", typeof(long));
            ...
        }
    }
}

Via reflection I build a list which contains all those classes with the static members. 通过反射,我构建了一个列表,其中包含所有带有静态成员的类。

Now I'd like to store somehow a "link" to one of these members in a property of another class. 现在,我想以某种方式将指向这些成员之一的“链接”存储在另一个类的属性中。

    [DefaultValue(null)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public kFieldClass Field
    {
        get; set;
    }

I've build a custom editor (which ist mostly based on this ) for the visual studio (which displays the hole list inside a treeview), which works fine. 我为视觉工作室(在树状视图内显示孔列表)构建了一个自定义编辑器(主要基于 )。

图片自定义编辑器

My problem now is, the designer can't store the static instance (kFieldClass) inside the Field property. 现在我的问题是,设计器无法在Field属性中存储静态实例(kFieldClass)。 So I think I've two options 所以我认为我有两种选择

  1. Make the "Field" property as string and store the info with dot-notation (kField.Campaign.Brand.id) and later get the static instance via reflection. 将“ Field”属性设置为字符串,并使用点符号(kField.Campaign.Brand.id)存储信息,然后通过反射获取静态实例。 Problem: Can't I have a custom editor for a string property? 问题:我不能为字符串属性使用自定义编辑器吗? Or is there any way? 还是有什么办法?

  2. Make some serializer for the property/kFieldClass so the designer can store the value. 为property / kFieldClass做一些序列化程序,以便设计人员可以存储该值。 I don't think this is possible nor the right way to do this. 我认为这是不可能的,也不是做到这一点的正确方法。

I've "played" with a custom TypeConverter for the property but I didn't have any luck regarding the interaction with the custom editor. 我已经为该属性使用了一个自定义的TypeConverter,但对于与该自定义编辑器的交互我没有任何运气。

In fact it is possible to add a custom designer to any property, so I've changed my property to string: 实际上,可以向任何属性添加自定义设计器,因此我已将属性更改为字符串:

    [DefaultValue(null)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [TypeConverter(typeof(kFieldTypeConverter))]
    [Editor(typeof(kFieldDesignerEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string Field 
    ...

So my static member is converted from and to string by reflection which is a bit messy but it works. 因此,我的静态成员通过反射从字符串转换为字符串,这有点混乱,但它可以工作。

That leaves this problem: The value in the property editor (the part which is visible in the VS property grid) is empty (but the property is filled, can be filled, and can be read). 这就留下了问题:属性编辑器中的值(在VS属性网格中可见的部分)为空(但属性已填充,可以填充并可以读取)。 However, this part can be removed completely: The TypeConverter is needless! 但是,可以完全删除此部分:无需使用TypeConverter!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM