drawtext,师范生招聘,西子书院局长成长史
public interface ianimal { } public interface idog : ianimal { } public class dog : idog { } public class cate : ianimal { } public class parrot { }
var ianimaltype = typeof(ianimal); var idogtype = typeof(idog); var dogtype = typeof(dog); var catetype = typeof(cate); var parrottype = typeof(parrot); console.writeline(ianimaltype.isassignablefrom(idogtype) ? $"{idogtype.name} was inherited from {ianimaltype.name}" : $"{idogtype.name} was not inherited from {ianimaltype.name}"); console.writeline(ianimaltype.isassignablefrom(dogtype) ? $"{dogtype.name} was inherited from {ianimaltype.name}" : $"{dogtype.name} was not inherited from {ianimaltype.name}"); console.writeline(idogtype.isassignablefrom(dogtype) ? $"{dogtype.name} was inherited from {idogtype.name}" : $"{dogtype.name} was not inherited from {idogtype.name}"); console.writeline(ianimaltype.isassignablefrom(catetype) ? $"{catetype.name} was inherited from {ianimaltype.name}" : $"{catetype.name} was not inherited from {ianimaltype.name}"); console.writeline(ianimaltype.isassignablefrom(parrottype) ? $"{parrottype.name} inherited from {ianimaltype.name}" : $"{parrottype.name} not inherited from {ianimaltype.name}"); 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
dog d=new dog(); var result=typeof(idog).isinstanceoftype(d); console.writeline(result? $"dog was inherited from idog": $"dog was not inherited from idog"); console.readkey();
输出结果:
dog was inherited from idog
public interface ianimal { } public interface idog : ianimal { } public class dog : idog { } public class husky : dog { } public class cate : ianimal { } public class parrot { }
husky husky = new husky(); var result = husky.gettype().issubclassof(typeof(dog)); console.writeline(result ? $"husky was inherited from dog" : $"husky was not inherited from dog");
输出结果:
husky was inherited from dog
这个方法不能用于接口,如果穿接口进去永远返回的都是false
dog dog = new dog(); var dogresult = dog.gettype().issubclassof(typeof(idog)); console.writeline(dogresult);
结果:
false
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论