How to use Activator to create an instance of a generic Type and casting it back to that type?

Since the actual type T is available to you only through reflection, you would need to access methods of Store<T> through reflection as well: Type constructedType = classType.MakeGenericType(typeParams); object x = Activator.CreateInstance(constructedType, new object[] { someParameter }); var method = constructedType.GetMethod(“MyMethodTakingT”); var res = method.Invoke(x, new object[] {someObjectThatImplementsStorable}); EDIT You could also define an additional … Read more

Java generic type

Yes, this can be done (sort of; see below). (In the C++ world, this is called the “Curiously Recurring Template Pattern“, but it applies in Java too): public interface Recur<T extends Recur<T>> { // … } (Note the second mention of T. That’s an essential part of the CRTP.) Also, this is how java.util.Enum is … Read more

Get derived class type from a base’s class static method

This can be accomplished easily using the curiously recurring template pattern class BaseClass<T> where T : BaseClass<T> { static void SomeMethod() { var t = typeof(T); // gets type of derived class } } class DerivedClass : BaseClass<DerivedClass> {} call the method: DerivedClass.SomeMethod(); This solution adds a small amount of boilerplate overhead because you have … Read more