Posted By: harumscarum | Jan 26th @ 1:48 PM
page 1 of 1
Comments: 7 | Views: 588
harumscarum
harumscarum
out of memory
I have been on a hiatus from .net for a while so I'm hoping I just missed the obvious. From a type passed into a method I am trying to create an object of this type in the method....so

public static List<IEye> GetSomeStuff(Type myType)
{
  object  = myType
}

Inside the method I am doing some refection based on the type passed in and I want to return the List of <myType>.
stevo_
stevo_
Human after all

Well, unless you specifically have a compile time type then you cannot expect the method to return that type.. only cast as it, of course you could use generics:

static List<T> GetSomeStuff<T>() where T : IEye
{
}

You can even do compile time enforcement of T having a public parameterless constructor by adding new() to the generic constraint..

You probably will still want to backup this method with one that takes a Type object also, because calling it without a compile time type will be impossible.

Is this what you are asking? or are you asking how to instanciate the type? in which case you need to use System.Activator

W3bbo
W3bbo
The Master of Baiters
I'm assuming the type myType refers to implements IEye.

You can create an instance of a type given the Type meta for that type, but that would just be a single instance. You haven't really described what the List will contain.

Anyway, you want this:

T retval = System.Activator.CreateInstance<T>();

or for the non-generic version (which is more flexible due to the overloads)

T retval = (T)System.Activator.CreateInstance(myType); (or typeof(T) )

Note that T must have a public constructor overload that matches the parameters passed to CreateInstance.
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
Where are you getting the T from? There is no T in harumscarum's code.

Essentially, you have two options, as indicated by W3bbo and stevo_.

The first is to make the method generic:
public static List<IEye> GetSomeStuff<T>() where T : IEye, new()<BR>{<BR>  IEye obj  = new T();<BR>}

This does however require that the caller knows the type at compile time (well, technically you can use reflection to construct the generic method invocation but that's even more complicated, not to mention slow).

The second is to use the Activator:
public static List<IEye> GetSomeStuff(Type myType)<BR>{<BR>  IEye obj  = (IEye)Activator.CreateInstance(myType);<BR>}

That's the option you should go with if the caller doesn't know the type at compile time.
stevo_
stevo_
Human after all
Just to note that technically the new() and using new T() on generics still uses Activator, its just that it gives you some compile time support to ensure the type has a public parameterless constructor.

I think the syntax for the new() was done somewhat specifically so potentially the future they could let you specify the type has a specific ctor.. but I don't think they have any plans at all to really do this.
stevo_
stevo_
Human after all

I would suggest reading up on generics to get you started on how they work, and perhaps linq to see a somewhat 'extreme' usage of generics (plus other things).

http://msdn.microsoft.com/en-us/library/512aeb7t.aspx

Make sure that you create a List of IEye and not a List of T or you will run into contravariance/covariance issues (which coincidentally won't be an issue in .NET 4.0).
page 1 of 1
Comments: 7 | Views: 588
Microsoft Communities