Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2007

Tip: Looking for answers? Try searching our database.

Activator.CreateInstance Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johnny R - 10 Jan 2007 15:46 GMT
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,
Mattias Sjögren - 10 Jan 2007 23:13 GMT
Johnny,

I don't see the connection to interop here, but still...

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

It depends on which overload you use. But the end result is that a new
object of the specified type is created and returned to you. It's like
using the new operator, but more dynamic.

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

Yes, just like when you use the new operator multiple times.

>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?

The garbage collector will take care of it, but not when a variable
referening the object goes out of scope. The object will be removed
some time after all references to it are gone.

>Code like below:
>
[quoted text clipped - 6 lines]
>
>Will Class created above deleted here when it comes out of scope?

No. Variable scope and object litefime are entirely different things
in .NET.

>Second question is that if I want that object created by
>Activator.CreateInstance
>is in memory permanently how will I do it?

Permanently meaning until the appdomain is unloaded? Referencing it
with a static variable should keep it alive.

>Is it possible to get reference to same instance using certain API?

No, if that's what you want you should probably use the factory
pattern.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.