Activator.CreateInstanceFrom

  • Thread starter Thread starter Wayne
  • Start date Start date
W

Wayne

If I wanted to create an instance of a System.Windows.Forms.Button how would
I go about doing this? I know if I specify the path to the DLL as the first
param, and the qualified name as the second all works well, but the DLL is
in the gac, is there any way to get Activator.CreateInstanceFrom to use the
GAC to find the DLL? and if so how do you specify the version? (or is that
why you wouldn't be able to use the gac?)

--
Thanks
Wayne Sepega
Jacksonville, Fl


"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
First of, if you wanted to create a System.Windows.Forms.Button
instance you just new it up like:

System.Windows.Forms.Button btn = new System.Windows.Forms.Button();

Soecondly, Activator.CreateXXXX is used for late binding calls. One way
you can create instances from GAC assemblies is to first load the
assemly and then call create instance. For ex:

Assembly assm = Assembly.Load ("MyAssembly, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=[something]");
MyType type = (MyType) assm.CreateInstance ("MyAssembly.MyType");

That will automatically probe the GAC to check for the MyAssembly
assembly.

You could also use Activator.CreateInstance function and specify the
assembly and type name. For GAC assemblies i dont think
CreateInstanceFrom will work.

NuTcAsE
 
Hi,

Just add
using System.Windows.Form;

you should have it already , but just in case.

you only use Activator to load a type at runtime, usually when you do not
know the real type at compile time, so you wait until runtime to decide
which exact typoe you want ot use.

cheers,
 
Thank you, that worked perfectly (once I figured out where to get the public
token).

BTW, the System.Windows.Forms.Button was just an example, I plan on using
this with my own assemblies. Also since it was already in the gac I figured
it simpler to test with instead of having to first create the assembly
strong name it and then put it in the GAC.
 
Back
Top