Reputation: 7569
Given this scenario
interface A {}
class B : A {}
A b = new B();
How can I check that object b is created from interface A?
Upvotes: 5
Views: 3340
Reputation: 11627
You could do test it like this:
var b = new B();
var asInterface = x as A;
if (asInterface == null)
{
//not of the interface A!
}
Upvotes: 5
Reputation:
We found it practical to use the following:
IMyInterface = instance as IMyInterface;
if (intance != null)
{
//do stuff
}
'as' is the faster than 'is', also is saves a number of casts - if your instance impelments IMyInterface, you'll need no more casts.
Upvotes: 0