Getting and using class hierarhy information in runtime

Adam D. Ruppe destructionator at gmail.com
Thu Feb 13 19:41:20 PST 2014


The easiest way is to simply use cast:

Exception e = ...;

if(auto b = cast(BaseHTTPException) e) {
    // use b of that type
} else if(auto s = cast(SpecializedHTTPException) e) {
    // use s, e is of this type
}

and so on.

On Friday, 14 February 2014 at 03:37:16 UTC, Uranuz wrote:
> Is it possible to check somehow at runtime that one object 
> inherits from another object somehow using .classinfo property.

classinfo also includes members for base class and a list of 
interfaces (in fact, internally, this is how the dynamic cast 
operator is implemented - it walks these chains and keeps track 
of the appropriate offset to convert the object).

But the cast will probably do what you need in a simpler way.


Also btw, the .classinfo and/or the typeid(obj) expressions will 
go straight to the most derived type. If you use that, you should 
be able to go into your associative array of handlers directly. 
But that won't call it for base classes so pros and cons.


More information about the Digitalmars-d-learn mailing list