如何確定ConstantExpression值爲null時的類型?我以前一直在使用下面的代碼檢測這個類型,但是當ConstantExpression Value爲null時它會導致一個空的異常。當Value爲null時從ConstantExpression獲取值類型
static Type GetType(Expression expression)
{
//causes exception when ((ConstantExpression)expression).Value is null
if (expression is ConstantExpression)
return ((ConstantExpression)expression).Value.GetType();
//Check other types
}
想象一下,我表達的創建與此類似: -
int? value = null;
ConstantExpression expression = Expression.Constant(value);
而且我想,以確定int類型?
'((常量表達式)表達)。 Type'? – Lee
謝謝,那很簡單!如果你添加一個答案,我可以接受它。 – Joe
供參考[正確的方法來檢查類型是否可爲空](http://stackoverflow.com/questions/8939939/correct-way-to-check-if-a-type-is-nullable) –