Hello,
I am building a generic class that needs to instanciate an object of its
template class, something like
public class MyGeneric<T>
{
List<TmyList = new List<T>();
void addToLst(List l)
{
foreach(MyObj ob in l)
{
myList.Add(new T(ob));
}
}
}
new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.
How to call a constructor for the type T used in the generic ?
Thanks for help.
CS 7 1548
On Aug 22, 4:40 pm, "WT" <W...@newsgroups.nospamwrote:
<snip>
new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.
How to call a constructor for the type T used in the generic ?
You can do new T(); if you've specified a parameterless constructor
constraint, but you can't specify particular constructor signatures -
you'd have to use Activator.CreateInstance (or similar). To use that,
just use typeof(T) instead of T in your code above.
Jon
WT,
If you want to be able to use new to create an instance of a generic
type parameter, then you have to add the constriaint, like so:
public class MyGeneric<Twhere T : new()
However, that won't work in this case as you need to be able to pass
parameters, and that only works for the parameterless constructor.
If you have to call a constructor with parameters (assuming there is no
other way to configure the instance without passing parameters through the
constructor) you can use typeof(T) to get the type of T, and then pass that
to the CreateInstance method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"WT" <WT@newsgroups.nospamwrote in message
news:uc**************@TK2MSFTNGP06.phx.gbl...
Hello,
I am building a generic class that needs to instanciate an object of its
template class, something like
public class MyGeneric<T>
{
List<TmyList = new List<T>();
void addToLst(List l)
{
foreach(MyObj ob in l)
{
myList.Add(new T(ob));
}
}
}
new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.
How to call a constructor for the type T used in the generic ?
Thanks for help.
CS
Thanks
"Jon Skeet [C# MVP]" <sk***@pobox.coma écrit dans le message de news: 11**********************@x35g2000prf.googlegroups. com...
On Aug 22, 4:40 pm, "WT" <W...@newsgroups.nospamwrote:
<snip>
>new T(ob) being refused, I tried Activator.CreateInstance(T, new Object[]{l}); but T is not a type.
How to call a constructor for the type T used in the generic ?
You can do new T(); if you've specified a parameterless constructor
constraint, but you can't specify particular constructor signatures -
you'd have to use Activator.CreateInstance (or similar). To use that,
just use typeof(T) instead of T in your code above.
Jon
Thanks
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.coma écrit
dans le message de news: ON**************@TK2MSFTNGP02.phx.gbl...
WT,
If you want to be able to use new to create an instance of a generic
type parameter, then you have to add the constriaint, like so:
public class MyGeneric<Twhere T : new()
However, that won't work in this case as you need to be able to pass
parameters, and that only works for the parameterless constructor.
If you have to call a constructor with parameters (assuming there is no
other way to configure the instance without passing parameters through the
constructor) you can use typeof(T) to get the type of T, and then pass
that to the CreateInstance method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"WT" <WT@newsgroups.nospamwrote in message
news:uc**************@TK2MSFTNGP06.phx.gbl...
>Hello,
I am building a generic class that needs to instanciate an object of its template class, something like
public class MyGeneric<T> { List<TmyList = new List<T>(); void addToLst(List l) { foreach(MyObj ob in l) { myList.Add(new T(ob)); } } }
new T(ob) being refused, I tried Activator.CreateInstance(T, new Object[]{l}); but T is not a type.
How to call a constructor for the type T used in the generic ?
Thanks for help. CS
Do you know if Activator method brings a huge penality in execution delay ?
CS
"Jon Skeet [C# MVP]" <sk***@pobox.coma écrit dans le message de news: 11**********************@x35g2000prf.googlegroups. com...
On Aug 22, 4:40 pm, "WT" <W...@newsgroups.nospamwrote:
<snip>
>new T(ob) being refused, I tried Activator.CreateInstance(T, new Object[]{l}); but T is not a type.
How to call a constructor for the type T used in the generic ?
You can do new T(); if you've specified a parameterless constructor
constraint, but you can't specify particular constructor signatures -
you'd have to use Activator.CreateInstance (or similar). To use that,
just use typeof(T) instead of T in your code above.
Jon
On Aug 23, 8:31 am, "WT" <W...@newsgroups.nospamwrote:
Do you know if Activator method brings a huge penality in execution delay ?
You'd have to test it to see whether it's significant for your usage.
For what it's worth, however, if you *do* specify the parameter
constructor constraint, the compiler just uses
Activator.CreateInstance behind the scenes anyway.
Jon
"WT" <WT@newsgroups.nospamwrote in message
news:u3**************@TK2MSFTNGP05.phx.gbl...
Do you know if Activator method brings a huge penality in execution delay
?
Probably substantial. If you anticipate calling more than once with the
same type and the argument types are fixed, then get the ConstructorInfo,
create a delegate, cache the delegate and call using that each time.
Delegate calls are the same performance as a v-table call, which is to say
quite fast.
>
CS
"Jon Skeet [C# MVP]" <sk***@pobox.coma écrit dans le message de news: 11**********************@x35g2000prf.googlegroups. com...
>On Aug 22, 4:40 pm, "WT" <W...@newsgroups.nospamwrote:
<snip>
>>new T(ob) being refused, I tried Activator.CreateInstance(T, new Object[]{l}); but T is not a type.
How to call a constructor for the type T used in the generic ?
You can do new T(); if you've specified a parameterless constructor constraint, but you can't specify particular constructor signatures - you'd have to use Activator.CreateInstance (or similar). To use that, just use typeof(T) instead of T in your code above.
Jon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Michael Kennedy [UB] |
last post by:
Hi Everyone,
I have this multithreaded C# windows forms application which does a lot of
image processing. Occasionally, I get the following error:
A generic error occurred in GDI+....
|
by: CoreyMas |
last post by:
This is what i am trying to do
I have a DAL that contains all of my database connections/reads etc.
I have a separate GUI layer that has my web pages
I would like to pass my exception errors...
|
by: Lasse Edsvik |
last post by:
Hello
Im trying to create a simple testclass that connects to a db on localhost
and a method that returns a dataset. I get these errors:
Unhandled Exception: System.InvalidOperationException:...
|
by: marco_segurini |
last post by:
Hi,
I like to know if is it possible to use sizeof(T) inside a generic
function.
I don't know why the following function compiles while if I define
ON_LINE_STATEMENT it does not.
...
|
by: Alphonse Giambrone |
last post by:
I am currently reading 'Programming The Web with Visual Basic .NET' and
have so far found it to be excellent. Downloaded all the code from Apress
and working in chapter 4, I get the error shown...
|
by: Brian Munroe |
last post by:
Let me preface this post: This is my first web app in ASP.NET, I am
sure I did things very kludgy, so please bare with my possibly stupid
ASP.NET design.
I am trying to create a custom error...
|
by: Konrad Kaczanowski |
last post by:
Hi all,
I'm creating code generator for wrappers of some c# classes. With the
introduction of c# 2.0 and generics the following problem arises.
When encountering generic types anywhere inside the...
|
by: Doug |
last post by:
Using Visual Studio 2005, SQL Server 2000, and ASP.NET/VB.NET for a Web
Application.
We have a System DSN using Windows NT authentication defined on the
development box to connect to the SQL...
|
by: akhilesh.noida |
last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting
errors while configuring it. Please check and give your inputs for
resolving this.
configure command :
$...
|
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: 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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |