如何在属性时覆盖自定义TypeConverter? [英] How do I override a custom TypeConverter when it is an attribute?

查看:98
本文介绍了如何在属性时覆盖自定义TypeConverter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我只是不知道如何以正确的方式向谷歌求助,但我不能为我的生活找到答案或方法来做到这一点:



我已经为我打算在Windows窗体设计器中使用的对象编写了一个基本控件,声明为抽象,在其中,我有一堆不抽象的属性。



其中一个使用我的转换器应用了TypeConverter属性。这是一个枚举的转换器,我将枚举的字符串分解为空格分隔的短语。



这个枚举的值对某些派生类有效,而对其他派生有效。有没有办法我可以把它作为基类并覆盖我需要的子控件?



原因是,所有的代码都是对于所有的孩子都一样,我不想将它从父母复制到所有孩子只是为了改变TypeConverter属性。



这是一个代码示例我希望这样做:



Maybe I just don't know how to ask google the right way but I cannot for the life of me find the answer or way to do this:

I have written a base control for objects I intend to use in the Windows Forms Designer, declared as abstract, where within it, I have a bunch of properties that are not abstract.

One of them has a TypeConverter attribute applied to it with my converter. This is a converter for an enum where I break the enum's strings into space delimited phrases.

This enum has values that are valid for some of the derived classes and not for others. Is there a way I can have this be a base class and override the pieces I need for the child controls?

The reason is, all the code for the proprety is the same for all the children and I would hate to copy it from the parent to all the children just to change the TypeConverter attribute.

Here is a code example of what I am looking to do:

public enum CustomEnum
{
    ValueNumber1,
    ValueNumber2,
}

public class CustomEnumConverter : TypeConverter
{
    public CustomEnumConverter()
    {
        supportedEnums = new ArrayList(Enum.GetValues(typeof(CustomEnum))
            .Cast<CustomEnum>().ToList());
    }

    (other code/functions implementing converter)

    private ArrayList supportedEnums;
}

// This will be used to remove "unsupported" values from the 
// supportedEnums ArrayList in the parent.
public class CustomClassBEnumConverter : CustomEnumConverter
{
}

public abstract class A : UserControl
{
    [Bindable(true)]
    // This can be different based on what child it is....
    [TypeConverter(typeof(CustomEnumConverter))]
    public CustomEnum MyBaseClassEnum
    {
        get { (code to return the value) };
        set { (code to set the value) };
    }
}

// So when an instance of this is dropped on a design surface, it will have 
// MyBaseClassEnum in the Forms Designer, but it may not have all the 
// enums available in the designer, only some of them.
public class B : A
{
}

推荐答案

Quote:

原因是,所有孩子的所有代码都是相同的,我不想将它从父母复制到所有孩子只是为了改变TypeConverter属性。

The reason is, all the code for the proprety is the same for all the children and I would hate to copy it from the parent to all the children just to change the TypeConverter attribute.



try使此属性虚拟


try to make this property virtual

public abstract class A : UserControl
{
    [Bindable(true)]
    // This can be different based on what child it is....
    [TypeConverter(typeof(CustomEnumConverter))]
    public virtual CustomEnum MyBaseClassEnum
    {
        get { (code to return the value) }
        set { (code to set the value) }
    }
}

// So when an instance of this is dropped on a design surface, it will have
// MyBaseClassEnum in the Forms Designer, but it may not have all the
// enums available in the designer, only some of them.
public class B : A
{
    // This can be different based on what child it is....
    [TypeConverter(typeof(CustomClassBEnumConverter))]
    public override CustomEnum MyBaseClassEnum
    {
        get { return base.MyBaseClassEnum; }
        set { base.MyBaseClassEnum = value; }
    }
}


这篇关于如何在属性时覆盖自定义TypeConverter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆