How do I access and use generic type parameters as a regular type in C#?

Try something like this: public abstract class BusinessObjectCollection : ICollection where T : BusinessObject, new() { // Here is a method that returns an instance // of type "T" public T GetT() { // And as long as you have the "new()" constraint above // the compiler will allow you to create instances of // "T" like this return new T(); } } In C# you can use the type parameter (i. E T ) as you would any other type in your code - there is nothing extra you need to do In order to be able to create instances of T (without using reflection) you must constrain the type parameter with new() which will guarantee that any type arguments contain a parameterless constructor.

Try something like this: public abstract class BusinessObjectCollection : ICollection where T : BusinessObject, new() { // Here is a method that returns an instance // of type "T" public T GetT() { // And as long as you have the "new()" constraint above // the compiler will allow you to create instances of // "T" like this return new T(); } } In C# you can use the type parameter (i.e. T) as you would any other type in your code - there is nothing extra you need to do. In order to be able to create instances of T (without using reflection) you must constrain the type parameter with new() which will guarantee that any type arguments contain a parameterless constructor.

Actually this does use reflection too. C# emits a call to Activator.CreateInstance. – Josh Einstein Feb 24 '10 at 22:54 That is very true that the compiler emits calls to Activator.CreateInstance.

I was simply stating that the OP would not have to use the reflection API directly, not that reflection was not used at all under the covers. – Andrew Hare Feb 25 '10 at 0:34.

With regards to finding a type: If you need to know what the type of T is, you can simply use typeof(T).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions