"Base class could not be loaded" - for no clear reason
Asked Answered
R

5

8

I'm using Visual C# 2008 Express. This is the first time I've had this error in years, the last time I had it was around the time I started programming.

I have no idea what it actually means or why it suddenly came up, it seems rather ambiguous for what the error message says. All I know is that throughout my app, I inherit a lot of the main windows from a ThemedWindow class I came up with to give them a custom appearance, there are no errors in that code and everything was working 100% perfect 20 minutes ago.

The error in full:

Warning 12 The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:

InheritingWindow --- The base class 'ThemedWindow' could not be loaded. Ensure the assembly has been referenced and that all projects have been built. 0 0

I get that for every form that inherits from ThemedWindow, and several other errors because the project won't build far enough for everything else to work.

Just before this happened I updated the font on all forms and sub-controls, the project even built and ran after I did that, then this happened for absolutely no clear reason.

I've tried the classic rebuild it, delete build files, reopen Visual Studio etc, but nothing seems to work at the moment.

Does anyone know how to fix this?

Rech answered 1/11, 2012 at 20:6 Comment(2)
Make sure ThemedWindow is not abstract, the designer does not like abstract base classes.Reinforce
possible duplicate of How to resolve VS2010 Windows Form Designer issue (base class cannot be loaded)Alcoholize
R
3

These problems were caused by an unfortunate bug in Visual Studio. I had other errors (not warnings) in some code, even though nothing else directly depended upon it, which played a part in this whole situation.

I could be wrong in explaining it but here's what I think went wrong: for some reason (bug), it totally forgot about unchanged objects it had already compiled before, gave me the actual errors I had in my code, and a bunch of other warnings because it wouldn't go past that point. I couldn't get into the designer for forms that depended upon ThemedWindow because I'm guessing ThemedWindow hadn't recompiled before these errors occurred.

Once I commented out the erroneous code as a quick fix and rebuilt the solution, everything compiled successfully.

Rech answered 2/11, 2012 at 0:11 Comment(3)
For me it worked to close Visual Studio, open it again in Administrator mode and rebuild again the solution and it worked.Beery
You say these problems were caused by..., speaking in the past tense as if this is no longer problem. This is a problem right now in the latest version of VS.Lerner
@AlinCiocan Restarting Visual Studio resolved it for me. Thanks.Mezcaline
D
18

Build your base usercontrol class with AnyCPU option.

Declivity answered 1/11, 2016 at 21:56 Comment(0)
E
4

As of writing this, this seems to be working properly in VS 2019 16.11.13.

It is not, however, working in VS 2022 17.1.6...



In VS 2022, I tried all the other fixes mentioned here, or they were not relevant:

  • AnyCPU is already set on the solution, never changed from that

  • The base class was never modified since it last worked, and so wouldn't need to recompile (plus this seems a moot point since it works fine in VS 2019)

  • While changing the base class temporarily to System.Windows.Forms.Form did get rid of the original error, it popped up a load of other errors of missing framework references and other non-problems.

  • The project is referencing .Net 4.8, (latest at time of writing this) and is not different from VS 2019 to VS 2022. Either way, can't reference non-existent newer version, and I doubt referencing an older version would fix this without breaking much more.

  • I saw a fix somewhere else of running VS as administrator and then rebuilding. This also did not fix the issue.


So I guess my fix for this is to downgrade from VS 2022 to VS 2019...
Emporium answered 6/5, 2022 at 18:42 Comment(1)
If you are currently building as x86, I have found that changing the build to Any CPU, rebuilding, and restarting VS2022 solves the problem. Ensuring that you are in Any CPU before starting the designer generally avoids the probleme completely.Hafiz
R
3

These problems were caused by an unfortunate bug in Visual Studio. I had other errors (not warnings) in some code, even though nothing else directly depended upon it, which played a part in this whole situation.

I could be wrong in explaining it but here's what I think went wrong: for some reason (bug), it totally forgot about unchanged objects it had already compiled before, gave me the actual errors I had in my code, and a bunch of other warnings because it wouldn't go past that point. I couldn't get into the designer for forms that depended upon ThemedWindow because I'm guessing ThemedWindow hadn't recompiled before these errors occurred.

Once I commented out the erroneous code as a quick fix and rebuilt the solution, everything compiled successfully.

Rech answered 2/11, 2012 at 0:11 Comment(3)
For me it worked to close Visual Studio, open it again in Administrator mode and rebuild again the solution and it worked.Beery
You say these problems were caused by..., speaking in the past tense as if this is no longer problem. This is a problem right now in the latest version of VS.Lerner
@AlinCiocan Restarting Visual Studio resolved it for me. Thanks.Mezcaline
D
0

In my case (VS 2015 Pro) none of the above helped. Only after changing the target framework to different (specifically newer, from 4.5.1 to 4.5.2) and recompiling did the Forms Editor load the form.

Before changing the target framework I tried cleaning the solution, removing and adding the references (the base user control was in different dll), manually deleting all bin and obj folders after cleaning the project, restarting VS, all to no avail...

Denby answered 24/11, 2016 at 12:18 Comment(0)
L
0

I have this problem too, with VS 2019 version 16.11.2, so I guess this bug will never be fixed.

My workaround, since my BaseForm class does all its work through code at runtime and has no functionality that would affect the designer, is that when I need to use the designer, I just temporarily change my window's base class. That is, change--

partial class MainWindow : BaseForm {

to

partial class MainWindow : Form {

then do the design stuff, and then once the designer is closed, put BaseForm back.

This may work for you if your base form class also doesn't do anything that would have an effect in the designer.

Laural answered 7/9, 2021 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.