Loading form at runtime using reflection

  • Thread starter Thread starter Saroj
  • Start date Start date
S

Saroj

Hi Gurus,
I need help.

Suppose I have a form called Form1 in c:\test folder.
How can I load this form directly without referencing a assembly.

I may not know which assembly this form belongs.

Is there any way to do this.
Any help/example/suggestion would be appreciated.

Thanks
Saroj
 
Saroj said:
Suppose I have a form called Form1 in c:\test folder.

And what are you regarding as a "form"?

A Form is nothing more than a Class (OK; it's a class that you can /see/
but it's still just a Class) and Classes only exist within Assemblies.
How can I load this form directly without referencing a assembly.

You can load the Assembly (Assembly.LoadFrom), then get the Form's Type
(Assembly.GetType) and feed the result into Activator.CreateInstance,
but ...
I may not know which assembly this form belongs.

.... you're going to have to find this out somehow.

At the very least, you're going to need the name (and path) of the file
that makes up the Assembly and the [fully-qualified] name of the [Form]
Class you want to instantiate.

HTH,
Phill W.
 
Perhaps the question involves compiling the form files at runtime? I have
seen various posts stating that it is possible to do such, but I have never
seen a need for this type of solution. Loading an assembly and finding
classes of course is the more common approach.

Phill W. said:
Saroj said:
Suppose I have a form called Form1 in c:\test folder.

And what are you regarding as a "form"?

A Form is nothing more than a Class (OK; it's a class that you can /see/
but it's still just a Class) and Classes only exist within Assemblies.
How can I load this form directly without referencing a assembly.

You can load the Assembly (Assembly.LoadFrom), then get the Form's Type
(Assembly.GetType) and feed the result into Activator.CreateInstance,
but ...
I may not know which assembly this form belongs.

.... you're going to have to find this out somehow.

At the very least, you're going to need the name (and path) of the file
that makes up the Assembly and the [fully-qualified] name of the [Form]
Class you want to instantiate.

HTH,
Phill W.
 
Thanks Guys.

Saroj

Family Tree Mike said:
Perhaps the question involves compiling the form files at runtime? I have
seen various posts stating that it is possible to do such, but I have
never
seen a need for this type of solution. Loading an assembly and finding
classes of course is the more common approach.

Phill W. said:
Saroj said:
Suppose I have a form called Form1 in c:\test folder.

And what are you regarding as a "form"?

A Form is nothing more than a Class (OK; it's a class that you can /see/
but it's still just a Class) and Classes only exist within Assemblies.
How can I load this form directly without referencing a assembly.

You can load the Assembly (Assembly.LoadFrom), then get the Form's Type
(Assembly.GetType) and feed the result into Activator.CreateInstance,
but ...
I may not know which assembly this form belongs.

.... you're going to have to find this out somehow.

At the very least, you're going to need the name (and path) of the file
that makes up the Assembly and the [fully-qualified] name of the [Form]
Class you want to instantiate.

HTH,
Phill W.
 
Back
Top