IsAssignableFrom vs IsSubclassOf

25/02/2008 - 14:34 por Rolando | Informe spam
Da lo mismo usar cualquiera de estas dos ?

1- if (UnTipo.IsAssignableFrom(OtroTipo))

2- if (OtroTipo.IsSubclassOf(UnTipo))

Preguntas similare

Leer las respuestas

#1 Alberto Poblacion
25/02/2008 - 19:15 | Informe spam
"Rolando" wrote in message
news:
Da lo mismo usar cualquiera de estas dos ?

1- if (UnTipo.IsAssignableFrom(OtroTipo))

2- if (OtroTipo.IsSubclassOf(UnTipo))



De la documentación de MSDN:

IsSubclassOf:
true if the Type represented by the c parameter and the current Type
represent classes, and the class represented by the current Type derives
from the class represented by c; otherwise, false. This method also returns
false if c and the current Type represent the same class.

IsAssignableFrom:
true if c and the current Type represent the same type, or if the current
Type is in the inheritance hierarchy of c, or if the current Type is an
interface that c implements, or if c is a generic type parameter and the
current Type represents one of the constraints of c. false if none of these
conditions are true, or if c is a null reference (Nothing in Visual Basic).


Como ves, IsAssignableFrom es true en varios casos en los que IsSubclassOf
no lo es. Entre ellos, y en particular, si Tipo y OtroTipo son el mismo
tipo, IsAssignableFrom devuelve true mientras que IsSubclassOf devuelve
false. También, si un tipo implementa un interfaz, es asignable a ese
interfaz, pero no se considera una subclase de ese interfaz.
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida