Dynamic Assemblies and Forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm trying to create a Windows Forms portal application and need some help.
What I currently have is a main application (.exe) containing an MDI form.
The idea is that this main application loads forms from packages (i.e. DLLs
containing one or more forms). The trick is that I'd like the form packages
and forms loaded to be dynamic. Therefore the main application should be
able to "discover" all the form packages, then allow those discovered forms
to be "shown" as MdiChildren. New forms should be able to be added and
discovered without ever having to recompile the main application. I know I
need to use dynamically loaded assemblies, but I'm not sure how and where to
start.

How would I go about doing this?

Thank you all for your help!

Sincerely,

Jon
 
JonS. said:
Hi,

I'm trying to create a Windows Forms portal application and need some help.
What I currently have is a main application (.exe) containing an MDI form.
The idea is that this main application loads forms from packages (i.e. DLLs
containing one or more forms). The trick is that I'd like the form packages
and forms loaded to be dynamic. Therefore the main application should be
able to "discover" all the form packages, then allow those discovered forms
to be "shown" as MdiChildren. New forms should be able to be added and
discovered without ever having to recompile the main application. I know I
need to use dynamically loaded assemblies, but I'm not sure how and where to
start.

How would I go about doing this?

Hi Jon,

I don't have neither VS or a manual at hand right now, so i will not give you a ready example now.
But check "Assembly" class, it should have a method "LoadAssembly(<DllFileName>)", which will return
an "Assembly" object associated with the dll. Then using Reflection, you can "extract" classes from the loaded asembly.

I will try to post something as i get to the office

Andrey
 
MuZZy said:
Hi Jon,

I don't have neither VS or a manual at hand right now, so i will not
give you a ready example now.
But check "Assembly" class, it should have a method
"LoadAssembly(<DllFileName>)", which will return
an "Assembly" object associated with the dll. Then using Reflection, you
can "extract" classes from the loaded asembly.

I will try to post something as i get to the office

Andrey

Ok, i found a piece of code i used for the task similar to yours - to dynamically load classes from assemblies:


// ====================== CODE START ============================================

// You shoul add this "using "
using System.Reflection;


// Here you load an assembly
private void btnOpenAssembly_Click(object sender, System.EventArgs e)
{
dlgOpen.Filter = "NET Assembly *.dll, *.exe | *.dll; *.exe";
if (dlgOpen.ShowDialog() != DialogResult.OK) return;
string fileName = dlgOpen.FileName;
// Read
Assembly asm = Assembly.LoadFrom(fileName);
Type[] asm_types = asm.GetTypes();
/*foreach(Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

foreach(MethodInfo m in mi)
{
m.Invoke(obj, null);
}
}*/
listClasses.Items.Clear();
textMethods.Text = "";
foreach(Type t in asm_types)
{
ListViewItem li = new ListViewItem(t.FullName);
li.Tag = t;
listClasses.Items.Add(li);
}
}


//Here you explore it's methods
private void listMethods_DoubleClick(object sender, System.EventArgs e)
{
if (listClasses.SelectedItems.Count == 0) return;
ListViewItem li = listClasses.SelectedItems[0];
Type t = (Type)li.Tag;
//BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
// BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MethodInfo[] mi = t.GetMethods();
textMethods.Text = "";
foreach (MethodInfo m in mi)
{
textMethods.Text += ' '+m.Name;
}
}

// ====================== CODE END ============================================


Code is very amature, but gives you a good start


Hope it helps,
Andrey
 
Thanks Muzzy,

I'm going to try it out and see what I come up with right now. Since the
object is essentially a form I guess I can use the MdiParent and Show methods
to get it to work dynamically.

What about the whole idea of being able to change/add DLLs (form packages)
while the actual software is running? Is this possible? From what I've read
they say it's doable using different App Domains. Do you know anything about
this?

MuZZy said:
MuZZy said:
Hi Jon,

I don't have neither VS or a manual at hand right now, so i will not
give you a ready example now.
But check "Assembly" class, it should have a method
"LoadAssembly(<DllFileName>)", which will return
an "Assembly" object associated with the dll. Then using Reflection, you
can "extract" classes from the loaded asembly.

I will try to post something as i get to the office

Andrey

Ok, i found a piece of code i used for the task similar to yours - to dynamically load classes from assemblies:


// ====================== CODE START ============================================

// You shoul add this "using "
using System.Reflection;


// Here you load an assembly
private void btnOpenAssembly_Click(object sender, System.EventArgs e)
{
dlgOpen.Filter = "NET Assembly *.dll, *.exe | *.dll; *.exe";
if (dlgOpen.ShowDialog() != DialogResult.OK) return;
string fileName = dlgOpen.FileName;
// Read
Assembly asm = Assembly.LoadFrom(fileName);
Type[] asm_types = asm.GetTypes();
/*foreach(Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

foreach(MethodInfo m in mi)
{
m.Invoke(obj, null);
}
}*/
listClasses.Items.Clear();
textMethods.Text = "";
foreach(Type t in asm_types)
{
ListViewItem li = new ListViewItem(t.FullName);
li.Tag = t;
listClasses.Items.Add(li);
}
}


//Here you explore it's methods
private void listMethods_DoubleClick(object sender, System.EventArgs e)
{
if (listClasses.SelectedItems.Count == 0) return;
ListViewItem li = listClasses.SelectedItems[0];
Type t = (Type)li.Tag;
//BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
// BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MethodInfo[] mi = t.GetMethods();
textMethods.Text = "";
foreach (MethodInfo m in mi)
{
textMethods.Text += ' '+m.Name;
}
}

// ====================== CODE END ============================================


Code is very amature, but gives you a good start


Hope it helps,
Andrey
 
JonS. said:
Thanks Muzzy,

I'm going to try it out and see what I come up with right now. Since the
object is essentially a form I guess I can use the MdiParent and Show methods
to get it to work dynamically.

What about the whole idea of being able to change/add DLLs (form packages)
while the actual software is running? Is this possible? From what I've read
they say it's doable using different App Domains. Do you know anything about
this?

I've never used App Domains yet, but i use the methods i've shown you for run-time adding/changing plugins.
Having ability to dynamically load/unload new assemblies you get what you want to achieve here

Let me know if the example worked for you or if you need some more explanations!

Andrey


:

MuZZy said:
JonS. wrote:


Hi,

I'm trying to create a Windows Forms portal application and need some
help. What I currently have is a main application (.exe) containing
an MDI form. The idea is that this main application loads forms from
packages (i.e. DLLs containing one or more forms). The trick is that
I'd like the form packages and forms loaded to be dynamic. Therefore
the main application should be able to "discover" all the form
packages, then allow those discovered forms to be "shown" as
MdiChildren. New forms should be able to be added and discovered
without ever having to recompile the main application. I know I need
to use dynamically loaded assemblies, but I'm not sure how and where
to start.

How would I go about doing this?


Hi Jon,

I don't have neither VS or a manual at hand right now, so i will not
give you a ready example now.
But check "Assembly" class, it should have a method
"LoadAssembly(<DllFileName>)", which will return
an "Assembly" object associated with the dll. Then using Reflection, you
can "extract" classes from the loaded asembly.

I will try to post something as i get to the office

Andrey

Ok, i found a piece of code i used for the task similar to yours - to dynamically load classes from assemblies:


// ====================== CODE START ============================================

// You shoul add this "using "
using System.Reflection;


// Here you load an assembly
private void btnOpenAssembly_Click(object sender, System.EventArgs e)
{
dlgOpen.Filter = "NET Assembly *.dll, *.exe | *.dll; *.exe";
if (dlgOpen.ShowDialog() != DialogResult.OK) return;
string fileName = dlgOpen.FileName;
// Read
Assembly asm = Assembly.LoadFrom(fileName);
Type[] asm_types = asm.GetTypes();
/*foreach(Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

foreach(MethodInfo m in mi)
{
m.Invoke(obj, null);
}
}*/
listClasses.Items.Clear();
textMethods.Text = "";
foreach(Type t in asm_types)
{
ListViewItem li = new ListViewItem(t.FullName);
li.Tag = t;
listClasses.Items.Add(li);
}
}


//Here you explore it's methods
private void listMethods_DoubleClick(object sender, System.EventArgs e)
{
if (listClasses.SelectedItems.Count == 0) return;
ListViewItem li = listClasses.SelectedItems[0];
Type t = (Type)li.Tag;
//BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
// BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MethodInfo[] mi = t.GetMethods();
textMethods.Text = "";
foreach (MethodInfo m in mi)
{
textMethods.Text += ' '+m.Name;
}
}

// ====================== CODE END ============================================


Code is very amature, but gives you a good start


Hope it helps,
Andrey
 
JonS. said:
Thanks Muzzy,

I'm going to try it out and see what I come up with right now. Since the
object is essentially a form I guess I can use the MdiParent and Show methods
to get it to work dynamically.

What about the whole idea of being able to change/add DLLs (form packages)
while the actual software is running? Is this possible? From what I've read
they say it's doable using different App Domains. Do you know anything about
this?

I've never used App Domains yet, but i use the methods i've shown you for run-time adding/changing plugins.
Having ability to dynamically load/unload new assemblies you get what you want to achieve here

Let me know if the example worked for you or if you need some more explanations!

Andrey


:

MuZZy said:
JonS. wrote:


Hi,

I'm trying to create a Windows Forms portal application and need some
help. What I currently have is a main application (.exe) containing
an MDI form. The idea is that this main application loads forms from
packages (i.e. DLLs containing one or more forms). The trick is that
I'd like the form packages and forms loaded to be dynamic. Therefore
the main application should be able to "discover" all the form
packages, then allow those discovered forms to be "shown" as
MdiChildren. New forms should be able to be added and discovered
without ever having to recompile the main application. I know I need
to use dynamically loaded assemblies, but I'm not sure how and where
to start.

How would I go about doing this?


Hi Jon,

I don't have neither VS or a manual at hand right now, so i will not
give you a ready example now.
But check "Assembly" class, it should have a method
"LoadAssembly(<DllFileName>)", which will return
an "Assembly" object associated with the dll. Then using Reflection, you
can "extract" classes from the loaded asembly.

I will try to post something as i get to the office

Andrey

Ok, i found a piece of code i used for the task similar to yours - to dynamically load classes from assemblies:


// ====================== CODE START ============================================

// You shoul add this "using "
using System.Reflection;


// Here you load an assembly
private void btnOpenAssembly_Click(object sender, System.EventArgs e)
{
dlgOpen.Filter = "NET Assembly *.dll, *.exe | *.dll; *.exe";
if (dlgOpen.ShowDialog() != DialogResult.OK) return;
string fileName = dlgOpen.FileName;
// Read
Assembly asm = Assembly.LoadFrom(fileName);
Type[] asm_types = asm.GetTypes();
/*foreach(Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

foreach(MethodInfo m in mi)
{
m.Invoke(obj, null);
}
}*/
listClasses.Items.Clear();
textMethods.Text = "";
foreach(Type t in asm_types)
{
ListViewItem li = new ListViewItem(t.FullName);
li.Tag = t;
listClasses.Items.Add(li);
}
}


//Here you explore it's methods
private void listMethods_DoubleClick(object sender, System.EventArgs e)
{
if (listClasses.SelectedItems.Count == 0) return;
ListViewItem li = listClasses.SelectedItems[0];
Type t = (Type)li.Tag;
//BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
// BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MethodInfo[] mi = t.GetMethods();
textMethods.Text = "";
foreach (MethodInfo m in mi)
{
textMethods.Text += ' '+m.Name;
}
}

// ====================== CODE END ============================================


Code is very amature, but gives you a good start


Hope it helps,
Andrey
 
Hey Muzzy,

Definitely worked displaying those forms, but I still need to figure the app
domain stuff out. Being able to add and remove packages of forms dynamically
is a must, and from what I've read I can't unload an assemblly, only app
domains, so I need to load these specific assemblies in a seperate app domain
and pass them over to my main app domain.

I'm going to keep looking for information, and hopefully come up with
something.

Sincerely,

Jon

MuZZy said:
JonS. said:
Thanks Muzzy,

I'm going to try it out and see what I come up with right now. Since the
object is essentially a form I guess I can use the MdiParent and Show methods
to get it to work dynamically.

What about the whole idea of being able to change/add DLLs (form packages)
while the actual software is running? Is this possible? From what I've read
they say it's doable using different App Domains. Do you know anything about
this?

I've never used App Domains yet, but i use the methods i've shown you for run-time adding/changing plugins.
Having ability to dynamically load/unload new assemblies you get what you want to achieve here

Let me know if the example worked for you or if you need some more explanations!

Andrey


:

MuZZy wrote:

JonS. wrote:


Hi,

I'm trying to create a Windows Forms portal application and need some
help. What I currently have is a main application (.exe) containing
an MDI form. The idea is that this main application loads forms from
packages (i.e. DLLs containing one or more forms). The trick is that
I'd like the form packages and forms loaded to be dynamic. Therefore
the main application should be able to "discover" all the form
packages, then allow those discovered forms to be "shown" as
MdiChildren. New forms should be able to be added and discovered
without ever having to recompile the main application. I know I need
to use dynamically loaded assemblies, but I'm not sure how and where
to start.

How would I go about doing this?


Hi Jon,

I don't have neither VS or a manual at hand right now, so i will not
give you a ready example now.
But check "Assembly" class, it should have a method
"LoadAssembly(<DllFileName>)", which will return
an "Assembly" object associated with the dll. Then using Reflection, you
can "extract" classes from the loaded asembly.

I will try to post something as i get to the office

Andrey

Ok, i found a piece of code i used for the task similar to yours - to dynamically load classes from assemblies:


// ====================== CODE START ============================================

// You shoul add this "using "
using System.Reflection;


// Here you load an assembly
private void btnOpenAssembly_Click(object sender, System.EventArgs e)
{
dlgOpen.Filter = "NET Assembly *.dll, *.exe | *.dll; *.exe";
if (dlgOpen.ShowDialog() != DialogResult.OK) return;
string fileName = dlgOpen.FileName;
// Read
Assembly asm = Assembly.LoadFrom(fileName);
Type[] asm_types = asm.GetTypes();
/*foreach(Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

foreach(MethodInfo m in mi)
{
m.Invoke(obj, null);
}
}*/
listClasses.Items.Clear();
textMethods.Text = "";
foreach(Type t in asm_types)
{
ListViewItem li = new ListViewItem(t.FullName);
li.Tag = t;
listClasses.Items.Add(li);
}
}


//Here you explore it's methods
private void listMethods_DoubleClick(object sender, System.EventArgs e)
{
if (listClasses.SelectedItems.Count == 0) return;
ListViewItem li = listClasses.SelectedItems[0];
Type t = (Type)li.Tag;
//BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
// BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MethodInfo[] mi = t.GetMethods();
textMethods.Text = "";
foreach (MethodInfo m in mi)
{
textMethods.Text += ' '+m.Name;
}
}

// ====================== CODE END ============================================


Code is very amature, but gives you a good start


Hope it helps,
Andrey
 
Back
Top