在 C# 中,我们可以在做其他事情之前确定 List 所持有的类型吗?例子:
List<int> listing = new List<int>();
if(listing is int)
{
// if List use <int> type, do this...
}
else if(listing is string)
{
// if List use <string> type, do this...
}
Run Code Online (Sandbox Code Playgroud)
可以用Type.GetGenericArguments()方法。
喜欢:
Type[] types = list.GetType().GetGenericArguments();
if (types.Length == 1 && types[0] == typeof(int))
{
...
}
Run Code Online (Sandbox Code Playgroud)