PC Review


Reply
Thread Tools Rate Thread

Activator.CreateInstanceFrom

 
 
Wayne
Guest
Posts: n/a
 
      8th Feb 2005
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


 
Reply With Quote
 
 
 
 
NuTcAsE
Guest
Posts: n/a
 
      8th Feb 2005
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

 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      8th Feb 2005
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Wayne" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Wayne
Guest
Posts: n/a
 
      8th Feb 2005
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.


"NuTcAsE" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Activator.CreateInstanceFrom(..) question Michael H Microsoft C# .NET 2 15th Feb 2006 11:23 AM
Activator Nice Chap Microsoft VB .NET 0 19th Jun 2004 04:01 PM
Activator.CreateInstanceFrom problem CKR Rajesh Microsoft C# .NET 15 7th Apr 2004 03:20 PM
Error calling _AppDomain->CreateInstanceFrom method dwp Microsoft VC .NET 1 22nd Sep 2003 03:45 AM
activator.exe? Brian Geer Windows XP General 2 28th Aug 2003 03:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:12 PM.