views:

853

answers:

7

I know this does not work, however does anyone have a way of making it work?

object obj = new object();
MyType typObj = new MyType();
obj = typObj;
Type objType = typObj.GetType();
List<objType> list = new List<objType>();
list.add((objType) obj);

EDIT:

Here is the current code: http://github.com/vimae/Nisme/blob/4aa18943214a7fd4ec6585384d167b10f0f81029/Lala.API/XmlParser.cs

The method I'm attempting to streamline is SingleNodeCollection

As you can see, it currently uses so hacked together reflection methods.

+5  A: 

It seems you're missing an obvious solution:

object obj = new object();
MyType typObj = new MyType();
obj = typObj;
List<MyType> list = new List<MyType>();
list.Add((MyType) obj);

If you really need the dynamic route, then you could do something like this:

object obj = new object();
MyType typObj = new MyType();
obj = typObj;
Type objType = typObj.GetType();

Type listType = typeof(List<>);
Type creatableList = listType.MakeGenericType(objType);

object list = Activator.CreateInstance(creatableList);
MethodInfo mi = creatableList.GetMethod("Add");
mi.Invoke(list, new object[] {obj});
Jeff Moser
Is this only in .Net 3.5?
Skizz
No, 2.0 and higher.
Jeff Moser
Nice bit of code, just solved a problem for me. :)
Jason Maskell
+1  A: 

You need reflection:

constructor = typeof (MyType).GetConstructor () // doing this from memory, the typeof might be wrong, I'm sure someone will edit it
typObj = (MyType) constructor.Invoke ()

It can also be done for generics but that is a bit trickier.

Skizz

Skizz
+1  A: 

You can do something like this using Generics, I'm not really sure what the point of it would be though.

    public List<T> TypedList<T>() where T : new()
 {
  object obj = new object();
  T typObj = new T();
  obj = typObj;
  List<T> list = new List<T>();
  list.Add((T)obj);
  return list;
 }
rmoore
A: 
object obj = new object();
Type objType = obj.GetType();
IList list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(objType));
list.Add(obj);

With this you will get an runtime error if you try to put something into list that is not assignable from objType.

Kleinux
A: 

I'm not entirely sure what you are trying to do, but would this work:

var obj = new MyType();

I might be misunderstanding your question though.

(I edited this to fix sample code which wouldn't compile, thanks for the comment)

marcc
var obj; is an invalid declaration. var is an implicit keyword, which means the compiler will determine what the type is. With the statement var obj; the compiler would not have enough info to determine the type.
Timothy Carter
A: 

Faster would be to use Reflection.Emit Here's a simple example of using Reflection.Emit for instantiating an arbitrary concrete type at runtime. For your purposes, you just need to have it call the ctor of List instead of T.ctor as in the example.

Krypes
A: 

Even though it seems answered, I still don't get it :)

Wouldn't it be useful to have the "typeToReturn" as generic argument to the function?

public List<T> SingleNodeCollection<T>(String xPath, XPathNavigator navigator)
  where T : new()
{
  XPathNodeIterator nodes = navigator.Select(xPath);
  List<T> returnedList = new List<T>(nodes.Count);
  ...
  T newObj = new T();
  ...
  Type t = typeof(T); // need the type anyway?
}
flq