经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# 中一些类关系的判定方法
来源:cnblogs  作者:蓝之风  时间:2018/12/27 9:53:52  对本文有异议

1.  IsAssignableFrom实例方法 判断一个类或者接口是否继承自另一个指定的类或者接口。

  1. public interface IAnimal { }
  2. public interface IDog : IAnimal { }
  3. public class Dog : IDog { }
  4. public class Cate : IAnimal { }
  5. public class Parrot { }
  1. var iAnimalType = typeof(IAnimal);
  2. var iDogType = typeof(IDog);
  3. var dogType = typeof(Dog);
  4. var cateType = typeof(Cate);
  5. var parrotType = typeof(Parrot);
  6.  
  7. Console.WriteLine(iAnimalType.IsAssignableFrom(iDogType)
  8. ? $"{iDogType.Name} was inherited from {iAnimalType.Name}"
  9. : $"{iDogType.Name} was not inherited from {iAnimalType.Name}");
  10.  
  11. Console.WriteLine(iAnimalType.IsAssignableFrom(dogType)
  12. ? $"{dogType.Name} was inherited from {iAnimalType.Name}"
  13. : $"{dogType.Name} was not inherited from {iAnimalType.Name}");
  14.  
  15. Console.WriteLine(iDogType.IsAssignableFrom(dogType)
  16. ? $"{dogType.Name} was inherited from {iDogType.Name}"
  17. : $"{dogType.Name} was not inherited from {iDogType.Name}");
  18.  
  19. Console.WriteLine(iAnimalType.IsAssignableFrom(cateType)
  20. ? $"{cateType.Name} was inherited from {iAnimalType.Name}"
  21. : $"{cateType.Name} was not inherited from {iAnimalType.Name}");
  22.  
  23. Console.WriteLine(iAnimalType.IsAssignableFrom(parrotType)
  24. ? $"{parrotType.Name} inherited from {iAnimalType.Name}"
  25. : $"{parrotType.Name} not inherited from {iAnimalType.Name}");
  26. Console.ReadKey();

输出结果:

IDog was inherited from IAnimal
Dog was inherited from IAnimal
Dog was inherited from IDog
Cate was inherited from IAnimal
Parrot not inherited from IAnimal

2.IsInstanceOfType 判断某个对象是否继承自指定的类或者接口

  1. Dog d=new Dog();
  2. var result=typeof(IDog).IsInstanceOfType(d);
  3. Console.WriteLine(result? $"Dog was inherited from IDog": $"Dog was not inherited from IDog");
  4. Console.ReadKey();

输出结果:

Dog was inherited from IDog

3.IsSubclassOf 判断一个对象的类型是否继承自指定的类,不能用于接口的判断,这能用于判定类的关系

  1. public interface IAnimal { }
  2. public interface IDog : IAnimal { }
  3. public class Dog : IDog { }
  4. public class Husky : Dog { }
  5. public class Cate : IAnimal { }
  6. public class Parrot { }
  1. Husky husky = new Husky();
  2. var result = husky.GetType().IsSubclassOf(typeof(Dog));
  3. Console.WriteLine(result ? $"Husky was inherited from Dog" : $"Husky was not inherited from Dog");

输出结果:

Husky was inherited from Dog

这个方法不能用于接口,如果穿接口进去永远返回的都是false

  1. Dog dog = new Dog();
  2. var dogResult = dog.GetType().IsSubclassOf(typeof(IDog));
  3. Console.WriteLine(dogResult);

结果:

false

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号