The designer must create an instance of…cannot because the type is declared abstract

前端 未结 3 891
小鲜肉
小鲜肉 2021-02-03 19:32

Visual Studio complains: Warning 1 The designer must create an instance of type \'RentalEase.CustomBindingNavForm\' but it cannot because the type is declared as abst

相关标签:
3条回答
  • 2021-02-03 20:14

    This worked for me with an abstract class inheriting a UserControl

    public class AbstractCommunicatorProvider : TypeDescriptionProvider
    {
        public AbstractCommunicatorProvider(): base(TypeDescriptor.GetProvider(typeof(UserControl)))
        {
        }
        public override Type GetReflectionType(Type objectType, object instance)
        {
            return typeof(UserControl);
        }
        public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
        {
            objectType = typeof(UserControl);
            return base.CreateInstance(provider, objectType, argTypes, args);
        }
    }
    
    
    [TypeDescriptionProvider(typeof(AbstractCommunicatorProvider))]
    public abstract partial class SelectorBase : UserControl
    {
    
    ///class contents 
    }
    

    I didn't need to add this to all derived classes but in your situation you may need to.

    0 讨论(0)
  • 2021-02-03 20:18

    I haven't seen the content at urban potato (its down) but Me and Smelch came up with a solution. Form itself inherits from an abstract class, so what they dont tell you is that its only the 1st level of inheritance that can't be abstract, the 2nd on down can.

    From there its simply a matter of having an empty class in the middle and wrapping an #if debug around the forms declaration and you're good to go. Just be sure to release in release mode and design in debug mode (which is very typical).

    You'll get full designer support and a real abstract base class at design (debug) and build (release) time because each time it ends up using your abstract base class.

    The full explanation and answer is here

    0 讨论(0)
  • 2021-02-03 20:27

    You can solve this using an attribute on your abstract class like the following

    [TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<MyBaseFormEf, Form>))]
    

    This will work for every case where you need it. The AbstractControlDescriptionProvider is below

    public class AbstractControlDescriptionProvider<TAbstract, TBase> : 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题