2012-07-12 80 views
7

No entiendo esta referencia a otro tipo' System.Collections.Generic.List`1 [CES.Model.SearchResult] 'y cómo resolverlo este problema.No se puede convertir el objeto de tipo '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(); 

Aquí está la clase searchResult y contiene la clase 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); 
    } 

} 

Gracias por su tiempo.

Respuesta

9

Tal vez esto debería funcionar:

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

De hecho, el mensaje déjame pensar que el XML contiene una colección de searchrestult, ni un solo resultado de la búsqueda.

[Editar] DJ KRAZE tiene razón, este código supone que la variable "resultados" es del tipo List<SearchResult>. El serializador debe coincidir con el tipo de objeto que se serializará.

+0

Steve es posible que desee explicar por qué está utilizando List en contra de él tratando de lanzar como SearchResult. esto puede ayudarlo a darse cuenta de que el casting debe ser del mismo tipo en su caso ... donde él tiene una variable privada definida como List <> + 1 para usted también – MethodMan

+0

Gracias Steve B. Otro caso de mí no leer el mensaje de error cuidadosamente . – greg

Cuestiones relacionadas