Use of IsAssignableFrom and "is" keyword in C#
Posted
by fearofawhackplanet
on Stack Overflow
See other posts from Stack Overflow
or by fearofawhackplanet
Published on 2010-06-10T10:53:33Z
Indexed on
2010/06/10
11:02 UTC
Read the original article
Hit count: 449
While trying to learn Unity, I keep seeing the following code for overriding GetControllerInstance
in MVC:
if(!typeof(IController).IsAssignableFrom(controllerType)) { ... }
this seems to me a pretty convoluted way of basically writing
if(controllerType is IController) { ... }
I appreciate there are subtle differences between is
and IsAssignableFrom
, ie IsAssignableFrom
doesn't include cast conversions, but I'm struggling to understand the implication of this difference in practical scenarios.
When is it imporantant to choose IsAssignableFrom
over is
? What difference would it make in the GetControllerExample
?
if (!typeof(IController).IsAssignableFrom(controllerType))
throw new ArgumentException(...);
return _container.Resolve(controllerType) as IController;
© Stack Overflow or respective owner