C# 不能应用于操作数';字符串';和';十进制';

C# 不能应用于操作数';字符串';和';十进制';,c#,unit-testing,C#,Unit Testing,我目前正在编写一些测试,看看字典中的值是否匹配。例如“TestOperatorMatchNotFoundIfValueNotNumeric”等。在比较字符串和字符串时,我已成功编写了测试,但现在我必须检查该值是否不是数字(使其无效) 我曾尝试使用之前测试中使用的逻辑进行一些调整,但我得到错误消息“Operator'==”不能应用于“string”和“decimal”类型的操作数。下面是我正在处理的代码,第8行和第9行有两条错误消息。非常感谢您的帮助 public bool IsMatch(Di

我目前正在编写一些测试,看看字典中的值是否匹配。例如“TestOperatorMatchNotFoundIfValueNotNumeric”等。在比较字符串和字符串时,我已成功编写了测试,但现在我必须检查该值是否不是数字(使其无效)

我曾尝试使用之前测试中使用的逻辑进行一些调整,但我得到错误消息“Operator'==”不能应用于“string”和“decimal”类型的操作数。下面是我正在处理的代码,第8行和第9行有两条错误消息。非常感谢您的帮助

 public bool IsMatch(Dictionary<String, String> variableData)
    {
        decimal outputValue;
        bool isANumber = Decimal.TryParse("StringValue", out outputValue);

        if (variableData.ContainsKey(this.value1Name))
        {
            if (comparisonOperator == "==")
            {
                if (variableData[this.value1Name] == this.value2Literal) //Error Msg
                {
                    return true;
                }
            }
            else
            {
                if (variableData[this.value1Name] != this.value2Literal) //Error Msg
                {
                    return true;
                }
            }
        }

        return false;
    }
public bool IsMatch(字典变量数据)
{
十进制输出值;
bool isANumber=Decimal.TryParse(“StringValue”,out outputValue);
if(variableData.ContainsKey(this.value1Name))
{
如果(比较运算符==“=”)
{
if(variableData[this.value1Name]==this.value2Literal)//错误消息
{
返回true;
}
}
其他的
{
if(variableData[this.value1Name]!=this.value2Literal)//错误消息
{
返回true;
}
}
}
返回false;
}
您无法比较1.00==“1.00”,您可以将.ToDecimal(“1.00”)转换为并比较它。 但我不建议这样做

你看你的字典有字符串值

所以你的代码会变成

Convert.ToDecimal(variableData[this.value1Name]) == this.value2Literal

value2Literal
comparisonOperator
Edit的类型是什么,value2Literal是一个小数,comparisonOperator是一个字符串。然后我100%确定上述代码中没有问题。把你出错的地方贴出来。