我如何确定两个对象的类型是否兼容? [英] How do I determine whether the types of two objects are compatible?

查看:111
本文介绍了我如何确定两个对象的类型是否兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



I have a generic function that I want to know how to write.

List<Something> something;

public int countItems<T>(List<T> Items)
{
    // Here I would like to compare the type of "Items" with the type of "something" to see if they are compatible. How do I do it?
    return 0;
}


推荐答案

p>

do you mean:

if(typeof(T) == typeof(Something)) {...}

请注意,泛型非常依赖于T(并采取不同的行为)意味着你所要做的并不是非常通用的 ...

Note that having generics depend hugely on the T (and act differently) may mean what you are trying to do isn't actually very generic...

这篇关于我如何确定两个对象的类型是否兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆