PC Review


Reply
Thread Tools Rate Thread

Activator.CreateInstance Question

 
 
Johnny R
Guest
Posts: n/a
 
      10th Jan 2007
Hello,

I'm loading a Class from Assemly DLL using Activator.CreateInstance.
That loaded Class is executed in a worker Thread with no loop.

What actually happends when class is loaded using Activator.CreateInstance?

If I create same class using Activator.CreateInstance many times will
there be multiple instances of that same class created by
Activator.CreateInstance?

If Activator.CreateInstance will create multiple classes when will those
instances of class be deleted? Do I have to use certain API to delete them
or will they be garbage collected after they are coming out of scope?

Code like below:

{ // Start of Block, scope begings
object baseObject = Activator.CreateInstance (type);
..
..
} // End of Block, scope ends
....

Will Class created above deleted here when it comes out of scope?

Second question is that if I want that object created by
Activator.CreateInstance
is in memory permanently how will I do it? Is it possible to get reference
to
same instance using certain API?

If anyone can give any answers or links to documents containing info I will
appraise it.

Cheers,


 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      11th Jan 2007
See answers inline...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Johnny R wrote:
> Hello,
>
> I'm loading a Class from Assemly DLL using Activator.CreateInstance.
> That loaded Class is executed in a worker Thread with no loop.
>
> What actually happends when class is loaded using Activator.CreateInstance?

Reflection is used to find the default parameterless constructor and to
run it.

>
> If I create same class using Activator.CreateInstance many times will
> there be multiple instances of that same class created by
> Activator.CreateInstance?


Yes.

>
> If Activator.CreateInstance will create multiple classes when will those
> instances of class be deleted? Do I have to use certain API to delete them
> or will they be garbage collected after they are coming out of scope?

Depends on the class. Like all objects you may have some resource issues
to clean up. You should decide whether to implement IDisposable for example.

Generally a simple class will be collected once there are no references
to it any more.


>
> Code like below:
>
> { // Start of Block, scope begings
> object baseObject = Activator.CreateInstance (type);
> ..
> ..
> } // End of Block, scope ends
> ...
>
> Will Class created above deleted here when it comes out of scope?

yes. Activation is no different really to the new keyword.

>
> Second question is that if I want that object created by
> Activator.CreateInstance
> is in memory permanently how will I do it? Is it possible to get reference
> to
> same instance using certain API?

You can use a singleton pattern for any object.

>
> If anyone can give any answers or links to documents containing info I will
> appraise it.
>
> Cheers,
>
>

 
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.CreateInstance =?Utf-8?B?Q29kZVJhem9y?= Microsoft C# .NET 1 26th Apr 2006 07:14 PM
Difference Between assembly.CreateInstance and System.Activator.CreateInstance John Jenkins Microsoft C# .NET 1 14th Oct 2004 10:00 PM
Activator.CreateInstance question =?Utf-8?B?c2Vhbg==?= Microsoft Dot NET 1 1st Apr 2004 06:08 AM
Difference Between CurrentDomain.CreateInstance and Activator.CreateInstance b0yce Microsoft Dot NET 0 23rd Mar 2004 03:56 PM
Difference Between CurrentDomain.CreateInstance and Activator.CreateInstance b0yce Microsoft C# .NET 0 22nd Mar 2004 04:12 PM


Features
 

Advertising
 

Newsgroups
 


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