Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support abstract base class in designer #3329

Closed
nawfalhasan opened this issue May 22, 2020 · 12 comments
Closed

Support abstract base class in designer #3329

nawfalhasan opened this issue May 22, 2020 · 12 comments
Labels
api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation area-VSDesigner Windows Forms out-of-proc designer related issues

Comments

@nawfalhasan
Copy link

nawfalhasan commented May 22, 2020

Is your feature request related to a problem? Please describe.
Problem: Unable to open up a form that inherits from an abstract form class.

Description:
When a form inherits from a abstract class, the designer of the more derived type cant be opened.

Example:

public abstract class FormBase : Form
{
    protected FormBase()
    {
    }
}

public class FormDerived : FormBase
{
    public FormDerived()
    {
    }
}

Now FormDerived in the designer wouldn't open up.

Error when I try today:
From the designer:

The designer must create an instance of type 'Xxx.Common.Presentation.Mvvm.FormBase' but it cannot because the type is declared as abstract.

Call stack:

at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)

Solution I would like:
Opening up the form designer should work any time from VS. Just like it opens up if base class is a regular class. The designer should design the first base class which can be instantiated (walking up the inheritance chain). In the above example it would be Form.

What I tried:
This is the most commonly seen hack on internet:

[TypeDescriptionProvider(typeof(GenericControlDescriptionProvider))]
public abstract class FormBase : Form
{
    protected FormBase()
    {
    }
}

class GenericControlDescriptionProvider : TypeDescriptionProvider
{
    public GenericControlDescriptionProvider()
        : base(TypeDescriptor.GetProvider(typeof(ContainerControl)))
    {
    }

    public override Type GetReflectionType(Type objectType, object instance)
    {
        Type designerType = GetTypeForDesigner(objectType);
        if (designerType != null)
        {
            return designerType;
        }

        return base.GetReflectionType(objectType, instance);
    }

    public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
    {
        Type designerType = GetTypeForDesigner(objectType);
        if (designerType != null)
        {
            objectType = designerType;
        }

        return base.CreateInstance(provider, objectType, argTypes, args);
    }

    static Type GetTypeForDesigner(Type actualTypeVsIsTryingToDesign)
    {
        if (actualTypeVsIsTryingToDesign.IsSubclassOf(typeof(UserControl)))
        {
            return typeof(UserControl);
        }

        if (actualTypeVsIsTryingToDesign.IsSubclassOf(typeof(Form)))
        {
            return typeof(Form);
        }

        return null;
    }
}

But then again it doesn't work flawlessly. Project has to be rebuilt sometimes, sometimes VS has to be restarted etc. Doesn't work always

@nawfalhasan nawfalhasan added the api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation label May 22, 2020
@weltkante
Copy link
Contributor

weltkante commented May 22, 2020

The designer needs to instantiate the base class. It can't instantiate your actual class because that would mean baking the code you are trying to edit in the designer (i.e. recompiling after every single edit)

It may be doable if the designer instantiated a dummy-subclass of the abstract base class.

@nawfalhasan
Copy link
Author

It may be doable if the designer instantiated a dummy-subclass of the abstract base class.

Either that, or let us have an option to control what designer should be designed. E.g. via an attribute like TypeDescriptionProvider. Unfortunately it doesnt always cleanly work.

@RussKie RussKie added the area-VSDesigner Windows Forms out-of-proc designer related issues label May 28, 2020
@merriemcgaw merriemcgaw added this to the Future milestone May 28, 2020
@merriemcgaw
Copy link
Member

We'll consider an addition like this once we're fully at parity with the Framework designer.

@nawfalhasan
Copy link
Author

@merriemcgaw thanks, can't wait! :)

@StevenBonePgh
Copy link

I believe this is somewhat related to a Developer Community issue that I raised recently. In my experiences shown there, this does sometimes work in that the designer loads, but requires doing a build, then a restart of Visual Studio for the designer to be able to load the derived control successfully. So I think it is fairly close to working, but not quite there (almost a year later).

@merriemcgaw
Copy link
Member

@Olina-Zhang can your team walk through the scenario and get us an issue in the designer repo to triage soon. You can also link @StevenBonePgh's Feedback ticket he raised for more details to the team. Thanks!

@John-Qiao
Copy link
Member

@merriemcgaw I have filed the GitHub issue: 3142 in designer repo based on details in StevenBonePgh's Feedback ticket. And the issue still occurs in the latest VS 2019 from main branch + latest .NET 6.0 SDK from main branch environment.

@RussKie
Copy link
Member

RussKie commented Feb 21, 2022

I acknowledge that the issue is known to the team, and it is on our backlog (https://github.com/dotnet/winforms-designer/issues/3142). I am afraid there is nothing else I can add to this at this point.

As a possible workaround you can add pragmas, to make the base form concrete during the development:

#if DEBUG
    public class FormBase : Form
#else
    public abstract class FormBase : Form
#endif
    {
        protected FormBase()
        {
        }
    }

Closing due to age.

@RussKie RussKie closed this as completed Feb 21, 2022
@msftbot msftbot bot removed this from the Future milestone Feb 21, 2022
@nawfalhasan
Copy link
Author

https://github.com/dotnet/winforms-designer/issues/3142 can't be opened. I get a 404.

image

@msftbot
Copy link
Contributor

msftbot bot commented Feb 21, 2022

Hi @nawfalhasan, it looks like you just commented on a closed issue. The team will most probably miss it.
If you have a question - consider opening a new discussion thread. Alternatively, you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

@weltkante
Copy link
Contributor

https://github.com/dotnet/winforms-designer/issues/3142 can't be opened. I get a 404.

The winforms designer is not a public repo, the team probably linked it for internal usage.

@msftbot
Copy link
Contributor

msftbot bot commented Feb 21, 2022

Hi @weltkante, it looks like you just commented on a closed issue. The team will most probably miss it.
If you have a question - consider opening a new discussion thread. Alternatively, you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

@msftbot msftbot bot locked as resolved and limited conversation to collaborators Mar 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation area-VSDesigner Windows Forms out-of-proc designer related issues
Projects
None yet
Development

No branches or pull requests

6 participants