我有以下代码,由于某种原因,我得到了 KeyNotFoundException,即使我使用的是我在上面检索了几行的密钥。有谁知道这不起作用的情况?我难住了。BTW 'SchemaElementType 是一个枚举。
public class DefaultValue
{
private Dictionary<Parameter, string> _params;
public DefaultValue(Dictionary<Parameter, string> parameters)
{
_params = parameters;
}
public string GetParameterValue(string name)
{
foreach(Parameter param in _params.Keys)
{
if(param.ParamName.Equals(name))
{
// **** Issue here ****
return _params[param];
}
}
return string.Empty;
}
}
[DataContract]
public class Parameter
{
#region Members
private Guid _guid;
private Guid _formulaGuid;
private string _name;
#endregion
#region Constructor
public Parameter(Guid guid, Guid formulaGuid, string name, SchemaElementType type)
{
ParamGuid = guid;
FormulaGuid = formulaGuid;
ParamName = name;
ParamType = type;
}
public Parameter()
{}
#endregion
#region Properties
[DataMember]
public Guid ParamGuid
{
get { return _guid; }
set { _guid = value; }
}
[DataMember]
public Guid FormulaGuid
{
get { return _formulaGuid; }
set { _formulaGuid = value; }
}
[DataMember]
public string ParamName
{
get { return _name; }
set { _name = value; }
}
[DataMember]
public SchemaElementType ParamType { get; set; }
#endregion
#region Overrides
public bool Equals(Parameter other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
bool result =other._guid.Equals(_guid);
result = result && other._formulaGuid.Equals(_formulaGuid);
result = result && Equals(other._name, _name);
result = result && Equals(other.ParamType, ParamType);
return result;
}
public override int GetHashCode()
{
unchecked
{
int result = _guid.GetHashCode();
result = (result*397) ^ _formulaGuid.GetHashCode();
result = (result*397) ^ (_name != null ? _name.GetHashCode() : 0);
result = (result*397) ^ ParamType.GetHashCode();
return result;
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Parameter)) return false;
return Equals((Parameter) obj);
}
#endregion
}