使用代码合同时,我收到警告:
在方法合同中检测到对方法“System.Int32.TryParse(System.String,System.Int32@)”的调用没有 [Pure]
具有在接口上定义的接口和代码协定的类,如下面的代码。问题是如何检查字符串是否orgNumberWithoutControlDigit
可以转换为有效整数,因为它是模数工作的先决条件?
public string getControlDigit(string orgNumberWithoutControlDigit)
{
List<int> orgNumberNumbers = this.getNumberList(orgNumberWithoutControlDigit);
List<int> productList = orgNumberNumbers.Zip(this.weightNumberList, (first, second) => first * second).ToList();
int modular = productList.Sum() % 11;
string controlDigit = getControlDigit(modular);
return controlDigit;
}
private static string getControlDigit(int modular)
{
string controlDigit;
if (modular == 0)
{
controlDigit = "0";
}
else if (modular == 1)
{
controlDigit = "-";
}
else
{
int result = 11 - modular;
controlDigit = result.ToString();
}
return controlDigit;
}
[ContractClass(typeof(CalculateOrgNumberControlDigitBusinessContract))]
public interface ICalculateOrgNumberControlDigitBusiness
{
string getControlDigit(string orgNumberWithoutControlDigit);
}
[ContractClassFor(typeof(ICalculateOrgNumberControlDigitBusiness))]
public abstract class CalculateOrgNumberControlDigitBusinessContract:ICalculateOrgNumberControlDigitBusiness
{
public string getControlDigit(string orgNumberWithoutControlDigit)
{
Contract.Requires(orgNumberWithoutControlDigit.Length == 8);
int parseResult;
Contract.Requires(int.TryParse(orgNumberWithoutControlDigit, out parseResult));
Contract.Ensures(parseResult >= 0);
var result = Contract.Result<string>();
Contract.Ensures(result != null && result.Length == 1);
return default(string);
}
}