c# - How do you cast an object to an interface it implements but doesn't extend? -
i have class script
, has several properties no methods. classes created extending script
. ideally script might have method update()
wouldn't extend interface iupdateable
(which has method 'update' specified). when script added object object realize that, while object doesn't extend iupdateable
, still implementation of it. add script list of scripts update methods, updated.
my question centres around 2 things, how can tell if class implements interface (without extending it), and, armed knowledge, how cast object update()
method can called.
are there other options should consider instead?
thanks
you can use reflection this.
to check if class implements interface, try below -
typeof(imyinterface).isassignablefrom(typeof(mytype)) typeof(mytype).getinterfaces().contains(typeof(imyinterface))
Comments
Post a Comment