473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Activator.CreateInstance() versus 'new'

Hi,

I need to design a method that creates and returns a large array of
objects. The problem is that the *type* of object to create isn't know until
runtime. As a result, a parameter of type "Type" must be passed in:

IMyInterface[] CreateObjects(Type type,int numberOfObjects);

Internally this method will call Activator.CreateInstance() to create
the objects. Here's my question: Is there a significant performance penalty
to calling Activator.CreateInstance() versus using "new" to create the
objects? If so, I'll look for an alternate way to accomplish this task.

I really wish that C# supported generics which would make this type of
task incredibly easy...

--
Sincerely,

David Sworder
http://www.CodeFanatic.com
Jul 21 '05 #1
18 3292

"David Sworder" <gi********@CSILasVegas.com> wrote in message
news:uS**************@TK2MSFTNGP11.phx.gbl...
Hi,

I need to design a method that creates and returns a large array of
objects. The problem is that the *type* of object to create isn't know
until
runtime. As a result, a parameter of type "Type" must be passed in:

IMyInterface[] CreateObjects(Type type,int numberOfObjects);

Internally this method will call Activator.CreateInstance() to create
the objects. Here's my question: Is there a significant performance
penalty
to calling Activator.CreateInstance() versus using "new" to create the
objects? If so, I'll look for an alternate way to accomplish this task.

It shouldn't be significant, no. While CreateInstance will probably take
longer to execute than the equivilent new calls, it should still be an
insignificant fraction of time, even if you are generating a considerable
number of objects. Run a test and see how long it takes to create the
maximum number of objects you expect. If you are generating *alot* of
objects, then perhaps a factory is more appropriate. Instead of loading x
number of a given object, write a factory class which can create the object,
load the factory with Activator.CreateInstance, and use the factories
CreateObject method to create a new object with whatever parameters you
need. All in all the factory approach may make for cleaner code as well, and
it gives you a bit more flexibility(you could, optionally, pool objects
inside the factory, using IDisposable.Dispose to release the object, change
the concrete type simply by changing the factory code, log creations, etc).
It does require more code for a given object type, and without knowing your
situation I can't say if it would be better or not.
I really wish that C# supported generics which would make this type of
task incredibly easy...
Generics are coming, but the support for generic construction is limited to
parameterless constructors, so its utility is there but less-so than it
would be in many cases. --
Sincerely,

David Sworder
http://www.CodeFanatic.com

Jul 21 '05 #2

"David Sworder" <gi********@CSILasVegas.com> wrote in message
news:uS**************@TK2MSFTNGP11.phx.gbl...
Hi,

I need to design a method that creates and returns a large array of
objects. The problem is that the *type* of object to create isn't know
until
runtime. As a result, a parameter of type "Type" must be passed in:

IMyInterface[] CreateObjects(Type type,int numberOfObjects);

Internally this method will call Activator.CreateInstance() to create
the objects. Here's my question: Is there a significant performance
penalty
to calling Activator.CreateInstance() versus using "new" to create the
objects? If so, I'll look for an alternate way to accomplish this task.

It shouldn't be significant, no. While CreateInstance will probably take
longer to execute than the equivilent new calls, it should still be an
insignificant fraction of time, even if you are generating a considerable
number of objects. Run a test and see how long it takes to create the
maximum number of objects you expect. If you are generating *alot* of
objects, then perhaps a factory is more appropriate. Instead of loading x
number of a given object, write a factory class which can create the object,
load the factory with Activator.CreateInstance, and use the factories
CreateObject method to create a new object with whatever parameters you
need. All in all the factory approach may make for cleaner code as well, and
it gives you a bit more flexibility(you could, optionally, pool objects
inside the factory, using IDisposable.Dispose to release the object, change
the concrete type simply by changing the factory code, log creations, etc).
It does require more code for a given object type, and without knowing your
situation I can't say if it would be better or not.
I really wish that C# supported generics which would make this type of
task incredibly easy...
Generics are coming, but the support for generic construction is limited to
parameterless constructors, so its utility is there but less-so than it
would be in many cases. --
Sincerely,

David Sworder
http://www.CodeFanatic.com

Jul 21 '05 #3
> > I really wish that C# supported generics which would make this type
of
task incredibly easy...
Generics are coming, but the support for generic construction is limited

to parameterless constructors, so its utility is there but less-so than it
would be in many cases.

Why is that???

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Jul 21 '05 #4
> > I really wish that C# supported generics which would make this type
of
task incredibly easy...
Generics are coming, but the support for generic construction is limited

to parameterless constructors, so its utility is there but less-so than it
would be in many cases.

Why is that???

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Jul 21 '05 #5
> Generics are coming, but the support for generic construction is limited
to
parameterless constructors, so its utility is there but less-so than it
would be in many cases.

I read an article about generics on
http://msdn.microsoft.com/msdnmag/issues/03/09/NET/ and this statement is
definitely wrong.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Jul 21 '05 #6
> Generics are coming, but the support for generic construction is limited
to
parameterless constructors, so its utility is there but less-so than it
would be in many cases.

I read an article about generics on
http://msdn.microsoft.com/msdnmag/issues/03/09/NET/ and this statement is
definitely wrong.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Jul 21 '05 #7

"cody" <pl*************************@gmx.de> wrote in message
news:eI**************@TK2MSFTNGP11.phx.gbl...
> I really wish that C# supported generics which would make this type of > task incredibly easy...
> Generics are coming, but the support for generic construction is limited

to
parameterless constructors, so its utility is there but less-so than it
would be in many cases.

Why is that???


Not sure, however, last I heard the only constraint related to constructors
you could place on a generic type is new().

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jul 21 '05 #8

"cody" <pl*************************@gmx.de> wrote in message
news:eI**************@TK2MSFTNGP11.phx.gbl...
> I really wish that C# supported generics which would make this type of > task incredibly easy...
> Generics are coming, but the support for generic construction is limited

to
parameterless constructors, so its utility is there but less-so than it
would be in many cases.

Why is that???


Not sure, however, last I heard the only constraint related to constructors
you could place on a generic type is new().

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jul 21 '05 #9

"cody" <pl*************************@gmx.de> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Generics are coming, but the support for generic construction is limited to
parameterless constructors, so its utility is there but less-so than it
would be in many cases.

I read an article about generics on
http://msdn.microsoft.com/msdnmag/issues/03/09/NET/ and this statement is
definitely wrong.


Hrm, I don't see anything at all related to generic construction here. This
article says *very* little about constraints in general. As far as the
public spec definiton goes, you have

public class GenericClass<T>
where T : new()
{
public T CreateObject()
{
return new T();
}
} --
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jul 21 '05 #10

"cody" <pl*************************@gmx.de> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Generics are coming, but the support for generic construction is limited to
parameterless constructors, so its utility is there but less-so than it
would be in many cases.

I read an article about generics on
http://msdn.microsoft.com/msdnmag/issues/03/09/NET/ and this statement is
definitely wrong.


Hrm, I don't see anything at all related to generic construction here. This
article says *very* little about constraints in general. As far as the
public spec definiton goes, you have

public class GenericClass<T>
where T : new()
{
public T CreateObject()
{
return new T();
}
} --
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jul 21 '05 #11
> Hrm, I don't see anything at all related to generic construction here.
This
article says *very* little about constraints in general. As far as the
public spec definiton goes, you have

public class GenericClass<T>
where T : new()
{
public T CreateObject()
{
return new T();
}
}

Oh I think I have misunderstood you.
I thought on something like that:

MyClass<int> = new MyClass<1234>();

So maybe the "generic construction" you were talking about is indeed limited
to parameterless ctors.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Jul 21 '05 #12
> Hrm, I don't see anything at all related to generic construction here.
This
article says *very* little about constraints in general. As far as the
public spec definiton goes, you have

public class GenericClass<T>
where T : new()
{
public T CreateObject()
{
return new T();
}
}

Oh I think I have misunderstood you.
I thought on something like that:

MyClass<int> = new MyClass<1234>();

So maybe the "generic construction" you were talking about is indeed limited
to parameterless ctors.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Jul 21 '05 #13

"cody" <pl*************************@gmx.de> wrote in message
news:uY**************@TK2MSFTNGP12.phx.gbl...
Hrm, I don't see anything at all related to generic construction here. This
article says *very* little about constraints in general. As far as the
public spec definiton goes, you have

public class GenericClass<T>
where T : new()
{
public T CreateObject()
{
return new T();
}
}

Oh I think I have misunderstood you.
I thought on something like that:

MyClass<int> = new MyClass<1234>();

So maybe the "generic construction" you were talking about is indeed
limited
to parameterless ctors.


Ahh, I see. Yes, normal generic class constructors are open to whatever you
want, and from what I gather the CLR is capable of handling generic
constructors with any number of parameters, the C# team just didn't choose
to expose constraint syntax for that particular situation.
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jul 21 '05 #14

"cody" <pl*************************@gmx.de> wrote in message
news:uY**************@TK2MSFTNGP12.phx.gbl...
Hrm, I don't see anything at all related to generic construction here. This
article says *very* little about constraints in general. As far as the
public spec definiton goes, you have

public class GenericClass<T>
where T : new()
{
public T CreateObject()
{
return new T();
}
}

Oh I think I have misunderstood you.
I thought on something like that:

MyClass<int> = new MyClass<1234>();

So maybe the "generic construction" you were talking about is indeed
limited
to parameterless ctors.


Ahh, I see. Yes, normal generic class constructors are open to whatever you
want, and from what I gather the CLR is capable of handling generic
constructors with any number of parameters, the C# team just didn't choose
to expose constraint syntax for that particular situation.
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jul 21 '05 #15
Talking abolut generics, is there any way to contraint the type parameter to
a reference type,
that means that I cannot pass value types?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Jul 21 '05 #16
Talking abolut generics, is there any way to contraint the type parameter to
a reference type,
that means that I cannot pass value types?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Jul 21 '05 #17

"cody" <pl*************************@gmx.de> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
Talking abolut generics, is there any way to contraint the type parameter
to
a reference type,
that means that I cannot pass value types?
Actually, yes, Eric Gunnerson blogged about it last month:
http://blogs.msdn.com/ericgu/archive.../24/95736.aspx

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jul 21 '05 #18

"cody" <pl*************************@gmx.de> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
Talking abolut generics, is there any way to contraint the type parameter
to
a reference type,
that means that I cannot pass value types?
Actually, yes, Eric Gunnerson blogged about it last month:
http://blogs.msdn.com/ericgu/archive.../24/95736.aspx

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jul 21 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: shmeian | last post by:
I have the following code which works fine. However I want to pass the object I'm instantiating a string for its constructor. I can't get the syntax right. Can someone give me an example of...
15
by: Brian Rogers | last post by:
Hello everyone, I apologize for the cross and re-post, but I am still searching for an answer. Why can C++ can create this object, but C# can't? I am trying to create an instance of the...
9
by: David Sworder | last post by:
Hi, I need to design a method that creates and returns a large array of objects. The problem is that the *type* of object to create isn't know until runtime. As a result, a parameter of type...
7
by: hazz | last post by:
this is a repost with more concise code (well, for me) and better questions (I hope....) . given the following two classes, my intent is to use either Activator.CreateInstance or InvokeMember pass...
3
by: Rob Richardson | last post by:
Greetings! I am trying to make it possible for new derived classes of an object to be used by my application without rebuilding the application. The new classes will be made known to my...
1
by: hazz | last post by:
this is a repost with a hopefully more clearly stated scenario and more concise questions at the end. given the following two classes, my intent is to use pass a token into the instantiated class...
1
by: Johnny R | last post by:
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...
0
by: Jon Skeet [C# MVP] | last post by:
On Apr 11, 8:40 am, Andrew <And...@discussions.microsoft.comwrote: Okay, that means you've either not given the full classname (including namespace) or it's not in mscorlib or the currently...
0
by: =?Utf-8?B?QW5kcmV3?= | last post by:
Found it. string name = Properties.Settings.Default.ClassName.ToString(); //"myproject.myclass, myassembly" format. //name = "ABC.MyClass, Assem" ; Type t = Type.GetType(name); Object obj...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.