我并不完全理解CASTing和序列化的所有复杂性,下面的代码在第二个对象的GetEnumerator()代码块中产生了一个强制转换错误,我遇到了问题。
无论是在解决这一问题方面,还是在更好地理解这一点方面,任何和所有的帮助都将受到极大的赞赏。提前谢谢。
[Serializable]
public class DBFieldMap
{
public String fieldName { get; set; }
public String fieldValue { get; set; }
public DBFieldMap() { }
public DBFieldMap(String fname, String fvalue)
{
fieldName = fname;
fieldValue = fvalue;
}
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
}第二对象
[Serializable]
public class MappedSQLFields : Dictionary<String, DBFieldMap>
{
public MappedSQLFields()
{
this.Add("clinicianstatus", new DBFieldMap());
this.Add("researcherstatus", new DBFieldMap());
this.Add("patientcarestatus", new DBFieldMap());
this.Add("managerstatus", new DBFieldMap());
this.Add("locationid", new DBFieldMap());
this.Add("managerid", new DBFieldMap());
}
public IEnumerator GetEnumerator()
{
return (IEnumerator)this; ==>Error message here is -Unable to cast object of type MappedSQLFields System.Collections.IEnumerator System.InvalidCastException
}
public MappedSQLFields(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}主要目标:
public Audience()
{
...
Dictionary<String, String> fields = new Dictionary<String, String>()
{
{"_locationid",""},
{"_managerid",""},
{"_clinicianstatus",""},
{"_managerstatus",""},
{"_patientcarestatus",""},
{"_researcherstatus",""},
};
private void loadCriteria()
{
//Load values into Dictionary
foreach (KeyValuePair<String,DBFieldMap> item in audienceSQLMap) ==>Error in stack starts here
{
this.fields["_"+ item.Key] = item.Value.fieldValue;
}
}
}发布于 2015-10-01 15:12:46
好的,DBFieldMap没有实现IEnumerable,所以我可以理解为什么那个转换会失败。
其次,MappedSQLFields已经通过继承Dictionary<String, DBFieldMap>实现了IEnumerable<KeyValuePair<String, DBFieldMap>>,所以我不知道为什么您觉得需要再次实现它。
https://stackoverflow.com/questions/32890991
复制相似问题