Activator.CreateInstance Question

J

Johnny R

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,
 
B

Bob Powell [MVP]

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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top