public void compare<T>(T someobject)
{
.....
.....
}
现在我想T
在运行时确定类型并根据它进行不同的操作。我试过使用typeof
但无济于事。
typeof 在运行时应该可以正常工作。
public void compare<T>(T someobject)
{
if (typeof(T) == typeof(int))
{
// do stuff
}
else if (typeof(T) == typeof(something else))
{
// do other stuff
}
}
is
可用于检查参数类型
if (someobject is SomeType)//...
或者,另一种...
Type constructedType = typeof(T);
if (constructedType == typeof(SomeType))//...
可以尝试
typeof(T) == typeof(desiredType)
例如:
typeof(T) == typeof(int)
if (someobj.GetType() == typeof(ClassA)) { /* do opertion */},