Setting Generic Type at Runtime

Setting generic type at runtime

You can't. Generic type identifiers have to be known at compile time.

edit

as of other posts, it appears to be possible by dynamicly generating the method and invoking it - which has dangers of course. See Thomas' and Dathan's posts for more inforation.

Passing a Type to a generic method at runtime

You can use reflection and construct your call like this:

Type MyType = Type.GetType(FromSomewhereElse);

var typeOfContext = context.GetType();

var method = typeOfContext.GetMethod("GetList");

var genericMethod = method.MakeGenericMethod(MyType);

genericMethod.Invoke(context, null);

Note that calling methods with reflection will add a huge performance penalty, try to redesign your solution if possible.

generic class, how to set the type in runtime?

You can create your class in another way, passing to the constructor the type that you want to use and exploit the dynamic keyword.

For example:

class MyGeneralClass
{
dynamic myVariable;
Type type;

public MyGeneralClass(Type type)
{
this.type = type;
myVariable = Activator.CreateInstance(type);
//And then if your type is of a class you can use its methods
//e.g. myVariable.MyMethod();
}

//If your function return something of type you can also use dynamic
public dynamic Function()
{
return Activator.CreateInstance(type);
}
}

How to set the generic type of an ArrayList at runtime in java?

You can't.

Generics in Java are simply compile-time syntactic sugar. It makes it so you don't have to cast everything to and from Object like we did in the old days when dinosaurs roamed the JVM, and gives you some compile-time type checking.

Edit to add: There is some metadata preserved at runtime that you can get at via reflection to inspect a generic class, but nothing like what you want.

C# : Making generic type at runtime

You are passing a Type into a method that expects a concrete class of BsonClassMap<T>

It seems you want

object classMapInstance = Activator.CreateInstance(type.Type);

Type unboundGeneric = typeof(BsonClassMap<>);
Type boundedGeneric = unboundGeneric.MakeGenericType(type.Argument);

// create the generic instance
object o = Activator.CreateInstance(boundedGeneric);

type.Handler.GetMethod("Configure").Invoke(classMapInstance, new object[] { o });

Note : Completely untested, and based entirely on my spidey senses

Build c# Generic Type definition at runtime

MakeGenericType - i.e.

Type passInType = ... /// perhaps myAssembly.GetType(
"ConsoleApplication2.Program+Person")
Type t = typeof(List<>).MakeGenericType(passInType);

For a complete example:

using System;
using System.Collections.Generic;
using System.Reflection;
namespace ConsoleApplication2 {
class Program {
class Person {}
static void Main(){
Assembly myAssembly = typeof(Program).Assembly;
Type passInType = myAssembly.GetType(
"ConsoleApplication2.Program+Person");
Type t = typeof(List<>).MakeGenericType(passInType);
}
}
}

As suggested in the comments - to explain, List<> is the open generic type - i.e. "List<T> without any specific T" (for multiple generic types, you just use commas - i.e. Dictionary<,>). When a T is specified (either through code, or via MakeGenericType) we get the closed generic type - for example, List<int>.

When using MakeGenericType, any generic type constraints are still enforced, but simply at runtime rather than at compile time.

Cast object to generic type at runtime

As TyCobb said, you have use even more reflection. Keep going until you get to some type that you can write the cast for, like string:

public static string GetTableName(this ObjectContext context, Type T)
{
var method = typeof(ObjectContext).GetMethod("CreateObjectSet", new Type[] { });
var generic = method.MakeGenericMethod(T);
var objectSet = generic.Invoke(context, null);

var toTrace = typeof(ObjectSet<>).MakeGenericType(T).GetMethod("ToTraceString");
var sqlString = (string)toTrace.Invoke(objectSet, null);

//...
}

Java Generics: Accessing Generic Type at runtime

It is true that generics aren't generally known at runtime in Java, because they are implemented with Type Erasure.

Reflecting Generics?

However, you can stil extract some valuable information about the declared types (NOT the runtime objects' types), as presented in Ian Roberston's article Reflecting Generics and Prenkov's article Java Reflection: Generics.

Background on Generics and Type Erasure

Generics where introduced while conserving backwards compatibility at the source qnd binary level, hence some of their limitation, like:

  • the impossibility to have a short-hand form without at least some indicator for generics support (here, the so-called diamond operator <>),
  • the impossibility to inspect generic-types at runtime, because they had to be implemented with Type Erasure.

Further Reading

  • From The Java Tutorial:

    • section on Generic Types
    • section on Type Inference and Instantiation of Generic Classes
  • From the Java Language Specifications (JLS):

    • Java SE 5's JLS section on Types, Values and Variables
    • Java SE 7's JLS section on Types, Values and Variables
  • From good StackOverflow questions:

    • Java Raw Type and generics interaction
  • Others:

    • IBM Developer Series: Java Theory and Practice: Generics Gotchas (especially the sections The Road Not Taken, Generifying Existing Classes and Implications of Erasure).


Related Topics



Leave a reply



Submit