Thread overview | |||||
---|---|---|---|---|---|
|
August 30, 2007 Type and interface checking | ||||
---|---|---|---|---|
| ||||
Hello! How can I check if an object is of a certain class, or implements a certain interface? Thanks Hans-Eric Grönlund http://www.hans-eric.com/ |
August 30, 2007 Re: Type and interface checking | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hans-Eric Grönlund | Hans-Eric Grönlund schrieb:
> Hello!
>
> How can I check if an object is of a certain class, or implements a certain interface?
>
> Thanks
>
> Hans-Eric Grönlund
> http://www.hans-eric.com/
Object o = ....;
// full qualified class name
o.classinfo.name
// test if castable to class or interface
MyType t = cast(MyType)o;
if( t !is null ){
// ok, it is castable
}
// short form
if( MyType t = cast(MyType)o ){
// ok, it is castable
}
|
August 30, 2007 Re: Type and interface checking | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hans-Eric Grönlund Attachments: | Hans-Eric Grönlund wrote:
> Hello!
>
> How can I check if an object is of a certain class, or implements a certain interface?
>
> Thanks
>
> Hans-Eric Grönlund
> http://www.hans-eric.com/
You can use the cast operator to determine if something can be cast to a type. See attached example.
Regards,
Myron.
|
Copyright © 1999-2021 by the D Language Foundation