www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Possible to get Class of Interface at runtime

reply Josh Phillips <jjpman71 gmail.com> writes:
If I have:

interface A {}
class B : A {}

void printClass(A obj){
     // Code here
}

Is there any way that I can find out what class obj is inside of 
printClass? I know I can cast and check if(cast(B)obj) but I want 
to just be able to do something along the lines of obj.class. I 
can think of other solutions but just wondering if something like 
this was possible.
Jan 22
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 22 January 2016 at 23:38:58 UTC, Josh Phillips wrote:
 Is there any way that I can find out what class obj is inside 
 of printClass?
There's a .classinfo property that works on Objects. If you have an interface, cast to Object first, and check for null, then get .classinfo off that.
Jan 22
parent reply Josh Phillips <jjpman71 gmail.com> writes:
On Friday, 22 January 2016 at 23:44:34 UTC, Adam D. Ruppe wrote:
 There's a .classinfo property that works on Objects.

 If you have an interface, cast to Object first, and check for 
 null, then get .classinfo off that.
I tried this but it will return A not B
Jan 23
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 23 January 2016 at 21:03:21 UTC, Josh Phillips wrote:
 I tried this but it will return A not B
Are you sure you correctly casted first?
Jan 23
parent Josh Phillips <jjpman71 gmail.com> writes:
On Saturday, 23 January 2016 at 21:06:32 UTC, Adam D. Ruppe wrote:
 Are you sure you correctly casted first?
Nope sorry. Thanks for the help!!
Jan 24
prev sibling parent ghjkl ghjkl.gh <ghjkl ghjkl.gh> writes:
On Saturday, 23 January 2016 at 21:03:21 UTC, Josh Phillips wrote:
 On Friday, 22 January 2016 at 23:44:34 UTC, Adam D. Ruppe wrote:
 There's a .classinfo property that works on Objects.

 If you have an interface, cast to Object first, and check for 
 null, then get .classinfo off that.
I tried this but it will return A not B
http://dpaste.dzfl.pl/f1bcf74d8cab
Jan 23