gpt4 book ai didi

.net - C#如何确定一个类型是否实现了给定的接口(interface)?

转载 作者:行者123 更新时间:2023-12-02 11:25:30 27 4
gpt4 key购买 nike

我希望以下内容返回 true :

var isIt = IsDisposable(typeof(TextWriter));

在哪里:
bool IsIDisposable(Type t){
???
// I tried:
//return t.IsSubclassOf(typeof(IDisposable)); //returns false
// the other t.IsXXX methods don't fit the requirement as far as I can tell
}

最佳答案

您可以使用 IsAssignableFrom

bool IsDisposable = typeof(IDisposable).IsAssignableFrom(typeof(TextWriter));

DEMO

关于.net - C#如何确定一个类型是否实现了给定的接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15138924/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com