12

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?

Community
  • 1
  • 1
scrot
  • 1,647
  • 8
  • 24
  • 31
  • This is not a direct answer to your question, but if you are going to do a lot of this you may want to check out the Composite Application Block (CAB). Its part of the Smart Client Software Factory and can be found here: http://msdn.microsoft.com/en-us/library/aa480482.aspx – blu Jul 06 '09 at 17:58

4 Answers4

15

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)
Juanma
  • 3,961
  • 3
  • 27
  • 26
3

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();
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
1

If a class belongs to Form then the Assembly.GetType() returns NULL. If a class belongs to User Control then I can see that the type is returned.

Also the syntax should be as:

Type type = assembly.GetType("Assemblytest.clsTest");

where

  • clsTest will be the name of class (of a user control)
  • Assemblytest is the name of assembly without the .dll extention.
Tisho
  • 8,320
  • 6
  • 44
  • 52
Uday
  • 11
  • 1
0

I would go with:

Assembly assembly = Assembly.LoadFile("C:\\test.dll");
Type type = assembly.GetType("test.dllTest");
object obj = Activator.CreateInstance(type);
Form form = obj as Form;
if (form != null)
    form.Show(); //or ShowDilaog() whichever is needed

Other error checking/handling should be added; however at the very least I would ensure the conversion works.

Timothy Carter
  • 15,459
  • 7
  • 44
  • 62
  • If you just want to do the null check, the extra object reference isn't needed you could get rid of obj and do the as cast directly on the createinstance. – Quintin Robinson Jul 06 '09 at 18:00
  • Depending on what your plan is in case of failure, you certainly can do that and eliminate a line of code. But if the check fails you plan to do/try something else with the object, then it needs to be done in separate lines. – Timothy Carter Jul 06 '09 at 18:07
  • Agreed, but in the second case I think you would delegate to a more pragmatic approach rather then an optimistic form cast and then a failsafe switch inline. – Quintin Robinson Jul 06 '09 at 18:11
  • Right I guess my main point was to ensure that error checking was addressed somewhere in this response chain. Obviously code posted here is not going to be production worthy robust, but I wanted it at least mentioned to a passerby in the future that some form of error checking should be added. And not to just assume the type you got is the type you thought it would be. If you can guarantee the type at compile time, why not reference the assembly and use the object; skip reflections altogether. – Timothy Carter Jul 06 '09 at 18:15
  • Like I said before.. agreed. Often text is so informal sincerity is easily lost. – Quintin Robinson Jul 06 '09 at 18:22