我不明白对另一种类型“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);
}
}
感谢您的宝贵时间。
最佳答案
也许这应该有效:
XmlSerializer xs = new XmlSerializer(typeof(List<SearchResult>));
实际上,该消息让我认为xml包含搜索结果的集合,而不是单个搜索结果。
[编辑] DJ KRAZE 是对的,此代码假定“结果”变量属于 List<SearchResult> 类型.序列化程序必须匹配它将序列化的对象类型。
关于c# - 无法转换类型为 'System.Collections.Generic.List` 1 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11461238/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在