I am trying to create forms that inherit from a "generic" base class where the generic argument of that base class has a constraint that it must implement one of my interfaces.
It compiles and runs just fine. My problem is with the Visual Studio designer. It will open the form fine until I rebuild the project, after which it then reports the error below when trying to view the form designer until such time as I either restart Visual Studio, or remove the custom interface constraint in the base class.
GenericArguments[0], 'InterfaceInBaseClassProblem.TestEntity', on 'InterfaceInBaseClassProblem.BaseWindowClass`1[EntityType]' violates the constraint of type 'EntityType'.
Step 1: Create an interface
namespace InterfaceInBaseClassProblem
{
public interface ITestInterface
{
void Func1();
}
}
Step 2: Create a class that implements the interface
namespace InterfaceInBaseClassProblem
{
public class TestEntity : ITestInterface
{
public void Func1()
{
}
}
}
Step 3: Create a generic base class Form that requires a generic parameter constrained to require an implementation of the interface we created
using System.Windows.Forms;
namespace InterfaceInBaseClassProblem
{
public class BaseWindowClass<EntityType> : Form where EntityType : ITestInterface
{
}
}
Step 4: Now create a new form that inherits from our Generic Base Form and uses the TestEntity class as the parameter
namespace InterfaceInBaseClassProblem
{
public partial class Form1 : BaseWindowClass<TestEntity>
{
public Form1()
{
InitializeComponent();
}
}
}
Step 5: Save your project and test for the following behaviour that I am trying to work around or solve:
a) Close Visual Studio
b) Open Visual Studio
c) Open the Form1 class to view its designer and confirm its working
d) Close the Form1 designer you just opened
e) Rebuild the project
f) Open the Form1 class to view the error I have reported.
g) Either restart Visual Studio, or comment out the "where EntityType : ITestInterface" constraint we included with step 3 and rebuild to see it start working again
Additional Notes:
In case Microsoft sees this, I'm getting a similar issue. Confirmed in VS2015. I have 4 forms:
public class BaseForm : Form
{
public BaseForm() {...}
}
public class BaseSubForm<T> : Form where T : BaseForm
{
public SubForm(T f) {...}
public InitForm(T f) {...}
}
public partial class TestForm : BaseForm
{
TestSubForm sf;
public TestForm()
{
...
sf = new TestSubForm(this);
}
}
public partial class TestSubForm : BaseSubForm<TestForm>
{
public TestSubForm(TestForm f) : base(f)
{
InitializeComponent();
}
}
If I build this, it'll run, but TestSubForm
won't display in the designer. However, if I remove the argument from BaseSubForm's constructor...
...
public SubForm() {...}
...
sf = new TestSubForm();
...
public TestSubForm(TestForm f)
...
I can restart Visual Studio, build, and it'll display just fine. I can get by on passing that argument in InitForm
, my 2nd stage constructor, but it's irritating I need 2 constructors.
Designer seems to hate classes of type Subclass : BaseClass<Generic>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With