As we know you can point to a constructor as a Func<T>
like this:
Func<MyObject> constructor = () => new MyObject();
var newObject = constructor();
But is there a way to make a constructor for an object you know inherits from MyObject, but you don't know its exact type?
Type inheritedObjectType = obj; // Parameter
Func<MyObject> constructor = () => new MyObject(); // as inheritedObjectType
var newInheritedObject = constructor; // Should now be of the inherited type
An answer with using Activator
or anything returning Object
is not an option.
Edit: I don't know what type the derived type is at compile time. I only have a System.Type
.