Could not load type "XYZ" from assembly

I wrote a fairly simple application that is an MDI window form. I am dynamically creating menu items for different forms to be loaded. When I click on the menu items, I call the ShowForm method, passing in the name of the form as a string. Then I try to open the form using the following code:

Dim strFullname As String = Application.ProductName & ".frm" & strFormName
Dim typForm As Type = Type.GetType(strFullname, True, True)
Dim CSFEMDIChild As Form = CType(Activator.CreateInstance(typForm), Form)

      

I am getting the following error executing the second line:

Could not load type 'MyAssemblyName.frmInquiryEntry' from assembly 'MyAssemblyName, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null'

I have read other questions related to this error, but most of them refer to custom assemblies. I looked in the GAC but I don't see an assembly that seems to be related to this application. I cleaned bin / x86 / debug so that it recreates all of these items. I don't even have links in this project other than the standard ones. Any ideas?

+3


source to share


1 answer


A simple mcve works fine with a winforms project with Form1 and Form2 and your code in Form1, but with

Dim strFullname As String = Application.ProductName & ".Form2"

      

So what is different? This is most likely a namespace issue. For example, if Form2 is defined in a namespace Foo

then the above code does not work, but it does work

Dim strFullname As String = Application.ProductName & ".Foo.Form2"

      

Actually Application.ProductName

what happens in your case is the root namespace. In my examples WindowsApplication1

and WindowsApplication1.Foo

are namespaces respectively.

So, you need to specify the namespace if you know that.



In my example, the current namespace (in which the form is defined) can be found and used

Dim currentNamespace = GetType(Form1).Namespace
Dim strFullname As String = currentNamespace & ".Foo.Form2"

      

and seems to be the same as the default assembly name

enter image description here

but most likely you have defined your child forms in a different namespace. If so, it might be wise to create a factory forms loader with a parameter that contains the namespace.

+1


source







All Articles