2012-07-12 34 views
7

この参照は別の型' System.Collections.Generic.List`1 [CES.Model.SearchResult] 'には理解できません。解決方法この問題。型 'System.Collections.Generic.List`1のオブジェクトをキャストすることができません

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); 
    } 

} 

ありがとうございます。

答えて

9

多分これは動作するはずです:実際には

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

を、メッセージには、私は、XMLはsearchrestultの集まりではなく、単一の検索結果が含まれていることを考えてみましょう。

[編集] DJ KRAZEが正しく、このコードは "results"変数が種類List<SearchResult>であると仮定しています。シリアライザーは、シリアライズするオブジェクトのタイプに一致する必要があります。

+0

スティーブは、SearchResultとしてキャストしようとしているのとは対照的に、List を使用している理由を説明したいことがあります。これは、キャスティングが自分のケースで同じ型のものでなければならないことを認識するのに役立つかもしれません..リストにあなたと同じようにプライベート変数が定義されています – MethodMan

+0

Thanks Steve B.エラーメッセージを慎重に読んでいない別のケース。 – greg

関連する問題