0

How can I use a Type object as a Type argument?

For example:

byte[] Data;
Type objectType;
public T MyFunction<T>()
{
     return OtherClass.Deserialize <objectType> (Data);
}

In other words, how do you use a Type object in a Type parameter <typehere>?

3
  • try <Type> instead of <myType> Commented Apr 25, 2013 at 5:20
  • I'm storing the type in a variable @FacioRatio Commented Apr 25, 2013 at 5:21
  • Also see this answer Commented Apr 25, 2013 at 5:22

5 Answers 5

1

Your generic method expects type identifier which is known at compile time. Meanwhile, you're trying to pass an instance of such a type to your method, but such an instance is not known until runtime.

Use reflection instead:

var method = typeof (OtherClass).GetMethods()
                                .Single(x => "Deserialize".Equals(x.Name) 
                                              && x.IsGenericMethodDefinition);
method = method.MakeGenericMethod(new[] { objectType });
method.Invoke(null, new object[] { Data });
Sign up to request clarification or add additional context in comments.

1 Comment

How would you suggest doing it? Perhaps I was unclear, I guess the real question is how to cast an object to a type stored in a Type variable?
0

Generics is really designed to work with types which are known at compile-time on the calling side. (The type argument can itself be a type parameter as far as the caller is concerned, of course.)

Otherwise, you're stuck with generics - you'd get the MethodInfo associated with OtherClass.Deserialize, call MakeGenericMethod using objectType as an argument, and then call the resulting method.

var method = typeof(OtherClass).GetMethod("Deserialize");
var genericMethod = method.MakeGenericMethod(objectType);
object result = genericMethod.Invoke(null, Data);

Note that this won't return a T though. It's not clear why your MyFunction method is generic if you're not actually going to use the type parameter.

Comments

0

Like this (recursively, for brevity):

Type myType;
public void MyFunction<T>(T instance) {
  MyFunction<Type>(myType);
}

3 Comments

This won't exactly work, I need to return a T type, not a Type object.
What are you exactly trying to do?
Get an object of type myType from a object object, sorry, struggling to be clear here.
0

here myType is a Type object. So you should call MyFunction<Type>( myType);

1 Comment

Not exactly sure what you're saying to do?
0

You should write MyFunction method like:

public T MyFunction<T>(byte[] data)
{
     return OtherClass.Deserialize<T>(data);
}

and the client code will look like:

byte[] Data = new byte[];
Type objectType;
objectType = MyFunction<Type>(Data);

2 Comments

Yeah but then the result is a Type object, this does absolutely no good, I need a object of type objectType..
Ah, I understand now what were you asking. Generics don't work with runtime determined types, they are intent to provide typesafety, which is not what you get with determining a type at runtime. Try to extract some common interface and use it with a generic method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.