www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Given a TypeInfo_Class, instance a new object of its type

reply Not A Rectangle <rahbek hurtigkode.dk> writes:
Hi everyone,
Say i have a TypeInfo_Class representing a custom type, and i 
want a new instance of this class with its custom type, is there 
a way to do that?

I am aware that TypeInfo_Class has the methods create() and 
factory(), both of which return a new object of the type Object.
I have tried to figure out a way to cast to the custom type using 
cast(...), but it seems i can't get the custom type held in 
TypeInfo in a way cast() will accept.

Is this possible to do?
Thanks
Oct 26 2020
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 26 October 2020 at 21:57:18 UTC, Not A Rectangle wrote:
 Is this possible to do?
Only if you know the type ahead of time, then you can cast it. Normally you'd probably just know an interface or base class it implements then you can cast to that, with the exact derived type being only known at runtime.
Oct 26 2020