How to

check for interface implementation

Published: 15. February 2012 | Updated: 15. February 2012
License: Microsoft Public License (MS-PL)
Categories: Framework » Basics
Tags: Basics C#
Was this snippet helpful for you? YESYES / NONO
object obj = GetObject();

// throws an InvalidCastException when 'obj' doesn't implement IDisposable interface
IDisposable disposable = (IDisposable)obj;

// never throws an exception
IDisposable disposable = obj as IDisposable;

// returns indication whether 'obj' has implemented IDisposable interface
bool implemented = obj is IDisposable;
Send us feedback about this snippet »



Related Snippets: