我得到了下面的代码,因为某些原因,我得到了一个KeyNotFoundException,即使我使用了一个我已经回顾了上面几行的密钥。有人知道这种情况不适用吗?我很难过。顺便说一句'SchemaElementType是一个枚举。使用先前从密钥集中检索的密钥时,获取KeyNotFoundException?
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
}
你有一些实际上显示问题的代码吗? – 2010-01-19 12:32:06