DevLab 的 CodeContract 似乎是一个不错的工具,但我在代码中有两个错误:
public class SomeClass
{
private DataTable _dataTable
// I don't want to write the same condition more then ones, so incapsulate it
private void CheckRowIndex(int rowIndex)
{
//Error1 in next line: User message to contract call can only be string literal, or a static
// field, or static property that is at least internally visible.
Contract.Requires<IndexOutOfRangeException>(_dataTable.Rows.Count >= rowIndex + 1,
String.Format("There is no row with index {0} in table.", rowIndex));
}
public object GetObject(int rowIndex, int colIndex)
{
// Error2 in next line: malformed contract
CheckRowIndex();
return _dataTable.Rows[rowIndex][colIndex];
}
public object GetObject(int rowIndex, string colName)
{
CheckRowIndex();
return _dataTable.Rows[rowIndex][colName];
}
}
有什么技巧可以避免吗?