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

前端 未结 3 923
小鲜肉
小鲜肉 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:27

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

    [TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider))]
    

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

    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);
        }
    }
    

提交回复
热议问题