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 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
"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
> > 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
> > 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
"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
"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
"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
"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
> 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
> 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
"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
"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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |