简体   繁体   中英

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. 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. 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. 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.

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). However, this part can be removed completely: The TypeConverter is needless!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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