我想找到Enumerable
的類型。我的代碼是這樣的:linq命令有什麼問題?
Type[] intLikeTypes = new[] { typeof(int), typeof(decimal), typeof(long), typeof(float) };
List<int> columnValue = new List<int>();
columnValue.Add(1);
columnValue.Add(2);
var listType = columnValue.GetType().GetGenericArguments()[0];
Type listGenericType = columnValue.GetType().GetGenericTypeDefinition();
if (listGenericType == typeof(List<>))
{
bool isInstanceOfTypeInt = (listType == typeof(int));
if (intLikeTypes.Any(x => x.IsInstanceOfType(listType)))
resColumnValue=preProcessValue(columnVal, false, false);
else if (listType is string)
resColumnValue=preProcessValue(columnVal, true, false);
}
當我使用
bool isInstanceOfTypeInt = (listType == typeof(int))
,該isInstanceOfTypeInt
是true
。但是,if(intLikeTypes.Any(x => x.IsInstanceOfType(listType))
的條件是false
。爲什麼x.IsInstanceOfType(listType)
無法正確找到實例?
順便說一下,linq
命令正在爲list<int>
類型以外的columnValue
工作。例如,它適用於int
類型。
如果你想比較類型只是做'intLikeTypes.Any(X => X == listType)'因爲X已經是一個'Type'。 – thepirat000
謝謝@ thepirat000!在建議的答案中看到我的評論! –
'intLikeTypes'的一個更好的名字是'numericTypes';) –