2

I have create a generic class that defines my main views:

public abstract class MainView<T, U> : UserControl, IMainView
    where T : ControllerMainView<U>
    where U : class
{
...
}

And I am using this to create a MainView of my Jobs

public partial class UserControlMainViewJobs : MainView<ControllerJobs, Job>
{
...
}

For this to work, ControllerJobs obviously needs to be a derivative of ControllerMain view, which it is.

public class ControllerJobs : ControllerMainView<Job>
{
...
}

Also, all classes have empty constructors, that the designer can use.

Still I can not open UserControlMainViewJobs in the Designer. Error Message is:

GenericArguments[0], 'FM.Auftragsverwaltung.Controller.ControllerJobs', on 'FM.Auftragsverwaltung.View.MainViews.MainView`2[T,U]' violates the constraint of type parameter 'T'.

Which makes no sense. But this seems to be general problem with generic user controls. Searching on StackOverFlow gave me this (and variations of it):

C# generics usercontrol

They suggest to create a static "intermediate class" to have a class that derives from MainView but is not deriving from a generic class.

So I create

public class MainViewJobsStatic : MainView<ControllerJobs, Job>
{
...
}

and then changed my UserControlMainViewJobs to

public partial class UserControlMainViewJobs : MainViewJobsStatic
{
...
}

Weirdly enough I still get the same error. Which makes me actually kind of happy since I find this workaround very frustrating.

Does anyone have an idea how to use the designer on a class that derives from a generic class?

* EDIT * Definition of ControllerMainView

public abstract class ControllerMainView<T> where T : class
{
...
}
Community
  • 1
  • 1
Jonidas
  • 187
  • 4
  • 21
  • 1
    What if you remove the abstract keyword and do a test, do you get the same error? – CodingYoshi Mar 17 '17 at 16:00
  • 1
    I think this `where T : ControllerMainView` is causing the problem. Can you show how `ControllerMainView` it's declared? – mrogal.ski Mar 17 '17 at 16:03
  • @CodingYoshi: Yes, I removed the abstract keyword and all "abstractness" and it resulted in the same error. – Jonidas Mar 17 '17 at 16:06
  • @m.rogalski: I added the declaration of ControllerMainView. – Jonidas Mar 17 '17 at 16:08
  • This might be the problem because of `partial` modifier. I've tested it and it works fine for me – mrogal.ski Mar 17 '17 at 16:13
  • @m.rogalski: this is really suprising to me. I created a completely new project with only the files specified above and I get the same error. Which version of visual studio do you use? – Jonidas Mar 20 '17 at 08:59
  • Starting from *VS 2015.1*, Windows Forms Designer shows classes which have generic base class without any problem. [See example](https://stackoverflow.com/a/49477226/3110834). – Reza Aghaei Dec 25 '19 at 17:20

0 Answers0