Abstract UserControl inheritance in Visual Studio designer

前端 未结 9 1168
广开言路
广开言路 2020-12-01 12:13
abstract class CustomControl : UserControl 
{
    protected abstract int DoStuff();
}

class DetailControl : CustomControl
{
    protected override int DoStuff()
            


        
9条回答
  •  有刺的猬
    2020-12-01 12:19

    What we want

    First, let's define the final class and the base abstract class.

    public class MyControl : AbstractControl
    ...
    public abstract class AbstractControl : UserControl // Also works for Form
    ...
    

    Now all we need is a Description provider.

    public class AbstractControlDescriptionProvider : TypeDescriptionProvider
    {
        public AbstractControlDescriptionProvider()
            : base(TypeDescriptor.GetProvider(typeof(TAbstract)))
        {
        }
    
        public override Type GetReflectionType(Type objectType, object instance)
        {
            if (objectType == typeof(TAbstract))
                return typeof(TBase);
    
            return base.GetReflectionType(objectType, instance);
        }
    
        public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
        {
            if (objectType == typeof(TAbstract))
                objectType = typeof(TBase);
    
            return base.CreateInstance(provider, objectType, argTypes, args);
        }
    }
    

    Finally we just apply a TypeDescriptionProvider attribute to the Abstract control.

    [TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider))]
    public abstract class AbstractControl : UserControl
    ...
    

    And that's it. No middle control required.

    And the provider class can be applied to as many Abstract bases as we want in the same solution.

提交回复
热议问题