WedTM
WedTM

Reputation: 2647

Dynamic typing in C#

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.

Upvotes: 7

Views: 5998

Answers (8)

Krypes
Krypes

Reputation: 561

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.

Upvotes: 0

rmoore
rmoore

Reputation: 15403

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;
}

Upvotes: 1

Skizz
Skizz

Reputation: 71110

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.

Upvotes: 1

Pawel Cioch
Pawel Cioch

Reputation: 3194

  public class myClass
  {
  }

  myClass instance = new myClass();

  Type t = instance.GetType;

//top is just to show obtaining a type...

public object GetListOfType(Type t)
{
  Type listType = typeof(List<>);
  var listOfType = listType.MakeGenericType(t);

  var listOfMyClassInstance = Activator.CreateInstance(listOfType); 

  return listOfMyClassInstance;
}

but eventually you have to cast... using your type directly

  List<object> listOfMyClass = GetListOfType(t);
  listOfMyClass.Add(myClassInstance);

  ((myClass)listOfMyClass[0]).SomeProperty

Upvotes: 0

flq
flq

Reputation: 22859

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?
}

Upvotes: 0

marcc
marcc

Reputation: 12399

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)

Upvotes: -2

Kleinux
Kleinux

Reputation: 1541

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.

Upvotes: 0

Jeff Moser
Jeff Moser

Reputation: 20053

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});

Upvotes: 10

Related Questions