using System;
namespace Articulos.Cap04.Excepciones.Parte5
{
public class A {}
public class B : A {}
public sealed class UsoInvalidCastException
{
public static void Main()
{
try
{
A a = new A();
// La conversión de superclase a subclase
// en una jerarquía de herencia no está
// permitada.
// El siguiente intento de conversión generará
// la excepción InvalidCastException:
B b = (B) a;
}
catch (InvalidCastException ice)
{
Console.WriteLine ("Mensaje de error: `{0}`", ice.Message);
}
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIEFydGljdWxvcy5DYXAwNC5FeGNlcGNpb25lcy5QYXJ0ZTUKewogICAgcHVibGljIGNsYXNzIEEge30KICAgIHB1YmxpYyBjbGFzcyBCIDogQSB7fQoKICAgIHB1YmxpYyBzZWFsZWQgY2xhc3MgVXNvSW52YWxpZENhc3RFeGNlcHRpb24KICAgIHsKICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCiAgICAgICAgewogICAgICAgICAgICB0cnkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQSBhID0gbmV3IEEoKTsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgLy8gTGEgY29udmVyc2nDs24gZGUgc3VwZXJjbGFzZSBhIHN1YmNsYXNlIAogICAgICAgICAgICAgICAgLy8gZW4gdW5hIGplcmFycXXDrWEgZGUgaGVyZW5jaWEgbm8gZXN0w6EgCiAgICAgICAgICAgICAgICAvLyBwZXJtaXRhZGEuCiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIC8vIEVsIHNpZ3VpZW50ZSBpbnRlbnRvIGRlIGNvbnZlcnNpw7NuIGdlbmVyYXLDoSAKICAgICAgICAgICAgICAgIC8vIGxhIGV4Y2VwY2nDs24gSW52YWxpZENhc3RFeGNlcHRpb246CiAgICAgICAgICAgICAgICBCIGIgPSAoQikgYTsKICAgICAgICAgICAgfQogICAgICAgICAgICBjYXRjaCAoSW52YWxpZENhc3RFeGNlcHRpb24gaWNlKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIk1lbnNhamUgZGUgZXJyb3I6IGB7MH1gIiwgaWNlLk1lc3NhZ2UpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9