-
TestCase testCase = new TestCase(row.TestCaseName, SWHandleUtility.ToHandle(row.TestCaseId)); testCase.Comment = row.Comment; testCase.IssueRef = row.IssueRef; if (string.IsNullOrWhiteSpace(row.PNo)) { testCase.PartNo = 0; } else { testCase.PartNo = row.PNo is int ? int.Parse(row.PNo) : long.Parse(row.PNo); } testCase.Result = row.Result = row.Result; testCase.TcId = row.TC_ID; testCase.LegacyId = row.LegacyId; currentSpecification.Children.Add(testCase); ++currentRow;
问题分析:
row.PNo为dynamic型,testCase.PartNo为string型,当row.PNo为空的时候,无法用Parse强制类型转换,所以需要把为空的情况给过滤掉。
问题解决:
通过使用IsNullOrWhiteSpace可以把为空的情况判断出来,然后把testCase.PartNo赋值为0,这样就进入else里面的语句了,程序就可以正常的运行。
问题拓展:
判断对象是否为Null和空白字符,而且性能更好,因为比较不会产生新String对象,而全用Trim().Length时全用Trim是会产生新的String对象
if (String.IsNullOrWhiteSpace())
{
}
值 | IsNullOrEmpty | IsNullOrWhiteSpace |
---|---|---|
null | true | true |
string.Empty | true | true |
"" | true | true |
" " | false | true |
String.IsNullOrWhiteSpace 方法 (String)
指示指定的字符串是 null、空还是仅由空白字符组成。