【问题标题】:Unable to cast object of type 'System.Collections.Generic.List`1无法转换类型为“System.Collections.Generic.List”的对象 1
【发布时间】:2012-07-12 21:49:46
【问题描述】:

我不明白对另一种类型“System.Collections.Generic.List`1[CES.Model.SearchResult]”的引用以及如何解决此问题。

Unable to cast object of type 'System.Collections.Generic.List`1[CES.Model.SearchResult]' to type 'CES.Model.SearchResult'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[CES.Model.SearchResult]' to type 'CES.Model.SearchResult'.

Source Error: 


Line 1069:            XmlSerializer xs = new XmlSerializer(typeof(SearchResult));
Line 1070:            TextWriter textWriter = new StreamWriter(@"C:\temp\results.xml");
Line 1071:            xs.Serialize(textWriter, results);
Line 1072:            ViewState["eventList"] = textWriter.ToString();
Line 1073:            textWriter.Close();

这里是 searchResult 类,它包含 SearchResultAttribute 类。

public class SearchResult
{
    private List<SearchResultAttribute> _attributes = null;
    private List<SearchResult> _linkedSearchResults = null;

    public string this[string key]
    {
        get
        {
            int resultIndex = _attributes.BinarySearch(new SearchResultAttribute(key, ""));

            if (resultIndex < 0)
                return "";
            else
                return _attributes[resultIndex].Value;
        }
        set
        {
            int resultIndex = _attributes.BinarySearch(new SearchResultAttribute(key, ""));

            if (resultIndex < 0)
                return;
            else
                _attributes[resultIndex].Value = value;
        }
    }

    public List<SearchResultAttribute> Attributes
    {
        get
        {
            return _attributes;
        }
        set
        {
            _attributes = value;
        }
    }
    public List<SearchResult> LinkedSearchResults
    {
        get
        {
            return _linkedSearchResults;
        }
        set
        {
            _linkedSearchResults = value;
        }
    }

    public SearchResult()
    {
        _attributes = new List<SearchResultAttribute>();
        _linkedSearchResults = new List<SearchResult>();
    }
}

public class SearchResultAttribute:IComparer<SearchResultAttribute>,IComparable<SearchResultAttribute>
{
    public string Key { get; set; }
    public string Value { get; set; }

    public SearchResultAttribute()
    {
        Key = System.String.Empty;
        Value = System.String.Empty;
    }

    public SearchResultAttribute(string key, string value)
    {
        Key = key;
        Value = value;
    }

    public int Compare(SearchResultAttribute x, SearchResultAttribute y)
    {
        return (x.Key.CompareTo(y.Key));
    }

    public int CompareTo(SearchResultAttribute other)
    {
        return this.Key.CompareTo(other.Key);
    }

}

感谢您的宝贵时间。

【问题讨论】:

    标签: c# .net xml list serialization


    【解决方案1】:

    也许这应该可行:

    XmlSerializer xs = new XmlSerializer(typeof(List<SearchResult>));
    

    其实这个消息让我觉得xml中包含searchrestult的集合,而不是单一的搜索结果。

    [Edit] DJ KRAZE 是对的,这段代码假设“results”变量是List&lt;SearchResult&gt;。序列化器必须匹配它要序列化的对象类型。

    【讨论】:

    • Steve,您可能想解释一下为什么您使用 List 反对他尝试将其转换为 SearchResult。这可能会帮助他意识到,在他的情况下,强制转换应该是相同的类型。他也将私有变量定义为 List + 1 给你
    • 感谢 Steve B。我没有仔细阅读错误消息的另一个例子。
    猜你喜欢
    • 1970-01-01
    • 2015-09-14
    • 2021-11-17
    • 1970-01-01
    • 2014-03-14
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多