The following doesn\'t work, of course. Is there a possible way, which is pretty similar like this?
Type newObjectType = typeof(MyClass);
var newObject = gi
I recently had the case, that I needed to generate some code like in Tomislav's answer. Unfortunately during generation time the type T was unknown. However, a variable containing an instance of that type was known. A solution dirty hack/ workaround for that problem would be:
public void CastToMyType(T hackToInferNeededType, object givenObject) where T : class
{
var newObject = givenObject as T;
}
Then this can be called by CastToMyType(instanceOfNeededType, givenObject) and let the compiler infer T.