Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a form from a dynamically loaded DLL

Tags:

c#

forms

dll

This is an extension of a question I previously asked here.

Long story short, I dynamically load a DLL and make a type out of it with the following code:

Assembly assembly = Assembly.LoadFile("C:\\test.dll");
Type type = assembly.GetType("test.dllTest");
Activator.CreateInstance(type);

From there I can use type to reference virtually anything in the dllTest class. The class by default when ran should bring up a form (in this case, fairly blank, so it's not complex).

I feel like I'm missing a key line of code here that's keeping the form from loading on the screen.

dllTest.cs (within the DLL) consists of:

namespace test
{
    public partial class dllTest : Form
    {
        public dllTest()
        {
            InitializeComponent();
        }
    }
}

InitializeComponent() sets up the layout of the form, which is far too long to paste here and shouldn't make a difference.

Any ideas?

like image 680
scrot Avatar asked Jul 06 '09 17:07

scrot


2 Answers

You have to do something with the form you've just created:

Assembly assembly = Assembly.LoadFile("C:\\test.dll");
Type type = assembly.GetType("test.dllTest");
Form form = (Form)Activator.CreateInstance(type);
form.ShowDialog(); // Or Application.Run(form)
like image 135
Juanma Avatar answered Oct 24 '22 01:10

Juanma


Yes, you aren't actually specifying any code to run outside the class initializer. For instance, with forms you have to actually show them.

You could modify your code to the following...

Assembly assembly = Assembly.LoadFile("C:\\test.dll");
Type type = assembly.GetType("test.dllTest");
Form form = Activator.CreateInstance(type) as Form;
form.ShowDialog();
like image 37
Quintin Robinson Avatar answered Oct 24 '22 01:10

Quintin Robinson