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

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

问题描述

我有一个通用函数,我想知道如何编写.

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;
}

推荐答案

你的意思是:

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天全站免登陆