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 / CLR / May 2004

Tip: Looking for answers? Try searching our database.

Type.GetType call cost

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jiho Han - 13 May 2004 18:08 GMT
Basically I am wondering how costly the call "Type.GetType()" is.
More specifically, I am wondering whether the subsequent identical calls
will incur the same amount of resource or not.

For example, let's say I have a MyClass type defined in MyAssembly.dll and I
call Type.GetType() method like below:

Type myType = Type.GetType("MyNamespace.MyClass, MyAssembly");

I assume then the the assembly will be loaded from the disk and the runtime
will use reflection to look up the type information, etc.  In my scenario,
this would be done in a static constructor/method, or in a one time setup
method.

Then some time later, let's say I need to create a class instance from the
same type but all I have is the same string argument that I passed into the
GetType method before - please don't argue why for the sake of argument for
now - and I use the same statement to create the Type instance and create an
instance at the same time like so:

Type myType = Type.GetType("MyNamespace.MyClass, MyAssembly");
MyClass myClass = (MyClass)Activator.CreateInstance(myType);

Would the second call to Type.GetType() incur the same amount of resource or
would it be relatively small since the assembly would have been loaded
already from the disk and cached?
Would calling, Activator.CreateInstance("MyAssembly",
"MyNamespace.MyClass"), make any difference?

Finally, MyAssembly.dll once loaded before, would it ever get unloaded from
the runtime other than by the appdomain recycle?

Thanks so much in advance.
Jiho
Justin Rogers - 14 May 2004 21:20 GMT
> Type myType = Type.GetType("MyNamespace.MyClass, MyAssembly");
>
> I assume then the the assembly will be loaded from the disk and the runtime
> will use reflection to look up the type information, etc.  In my scenario,
> this would be done in a static constructor/method, or in a one time setup
> method.

Grabbing the Type of an object is just going based on metadata.  If the assembly
is not already loaded, then yes that is a cost and the assembly will be loaded.
Unless you access a static member of a type, then no static constructors won't
get run.  Each call to GetType is going to incur the cost of probing the type,
and
I don't think that the result of the call is cached in any way (I could be
wrong, but
what I'm seeing in rotor points that it is not cached).

> Then some time later, let's say I need to create a class instance from the
> same type but all I have is the same string argument that I passed into the
[quoted text clipped - 4 lines]
> Type myType = Type.GetType("MyNamespace.MyClass, MyAssembly");
> MyClass myClass = (MyClass)Activator.CreateInstance(myType);

It will be smaller, since the assembly is loaded.  However, the full walk for
the
type's information is still undergone.  It is cheaper to store the Type
somewhere in
your application if you think you are going to be using it a bunch.

> Finally, MyAssembly.dll once loaded before, would it ever get unloaded from
> the runtime other than by the appdomain recycle?

No it won't get unloaded unless the AppDomain is unloaded.  Even then I think
there are assembly types that may exist across the AppDomain boundary (neutral
assemblies or something) that wouldn't be removed until the process exits.

Signature

Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Jiho Han - 14 May 2004 21:53 GMT
Thanks Justin.

I think I am going to keep the types around instead of the string.

Jiho

> > Type myType = Type.GetType("MyNamespace.MyClass, MyAssembly");
> >
[quoted text clipped - 33 lines]
> there are assembly types that may exist across the AppDomain boundary (neutral
> assemblies or something) that wouldn't be removed until the process exits.
Eliahu Baker - 17 May 2004 11:16 GMT
For our application we can see that first Type.GetType for specific time take some time, while preciding calls are quick.

Rate this thread:







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.