简体   繁体   English

CBO.FillCollection抛出“没有为此对象定义的无参数构造函数。”错误

[英]CBO.FillCollection throwing “No parameterless constructor defined for this object.” error

I'm trying to fill a collection from an IDataReader that was returned by another method... for some reason it keeps throwing a "No parameterless constructor defined for this object." 我正在尝试从另一个方法返回的IDataReader中填充一个集合...由于某种原因,它不断抛出“为此对象定义的无参数构造函数”。 error for this line: 此行的错误:

List<string> names = CBO.FillCollection<string>(DataProvider.Instance().ExecuteReader("getNames", new SqlParameter("UserId", 1)));

I've tried separating out the parameters so things get initialized separately until I had this: 我已经尝试分离出参数,所以事情会分开初始化,直到我有了这个:

List<string> names = CBO.FillCollection<string>(nameDataReader);

and I was still getting an error on the same line. 我在同一条线上仍然收到错误。

Any ideas? 有任何想法吗?

The clue's in the message. 消息中的线索。 There's no parameterless constructor for System.String , so it can't be created using Activator.CreateInstance , which is what is usually used to dynamically create objects. System.String没有无参数构造函数,因此无法使用Activator.CreateInstance创建它,这通常用于动态创建对象。

EDIT : A solution would be to use the reader directly: 编辑 :解决方案是直接使用阅读器:

var strings = new List<string>();
using(var reader = DataProvider.Instance().ExecuteReader("getNames", new SqlParameter("UserId", 1)))
{
    while(reader.Read()) 
        strings.Add(reader[0] as string);
}

CBO.FillCollection seems to have an issue with value types. CBO.FillCollection似乎与值类型有关。

The better answer is already posted (access the reader directly), but to understand what the FillCollection method is looking for, you could have solved your problem with this: 已经发布了更好的答案(直接访问阅读器),但要了解FillCollection方法正在寻找什么,您可以用以下方法解决您的问题:

Add a new class with a property set to your SQL column name: 添加一个新属性,其属性设置为SQL列名:

class StringRow { public string Name; } 

Use it to pass to FillCollection, like: 使用它传递给FillCollection,如:

List<string> stringCollection = new List<string>();
foreach (StringRow row in CBO.FillCollection<StringRow>(DataProvider...)) 
{
    stringCollection.Add(row.Name);
}

It wants an object with a named property it can reflectively set. 它想要一个具有命名属性的对象,它可以反射设置。 So even if you were retrieving a list of int , the int object doesn't have a 'Name' property (pulled from the column in the SQL return set) to set, in which case it would return a list of 0s. 因此,即使您正在检索int列表, int对象也没有“Name”属性(从SQL返回集中的列中拉出)来设置,在这种情况下,它将返回0的列表。

Unfortunately, using int? 不幸的是,使用int? and setting the SQL return column to [Value] does not remedy the solution. 并将SQL返回列设置为[Value]并不能解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取“未为此对象定义无参数的构造函数。”发布视图模型时出错 - Getting “No parameterless constructor defined for this object.” Error while posting a viewmodel 错误“未为此对象定义无参数的构造函数。” ViewModel中的SelectList错误 - Error “No parameterless constructor defined for this object.” with SelectList from ViewModel Unity:没有为此对象定义无参数构造函数。 - Unity : No parameterless constructor defined for this object. 没有为此对象定义无参数构造函数。 MVC 5 - No parameterless constructor defined for this object. MVC 5 错误:未为此对象定义无参数构造函数 - Error: No parameterless constructor defined for this object 没有为此对象定义无参数构造函数。 当我尝试使用 Unity 依赖时 - No parameterless constructor defined for this object. while i try for Unity Dependency System.MissingMethodException:没有为此对象定义的无参数构造函数。 MVC4 - System.MissingMethodException: No parameterless constructor defined for this object. MVC4 “没有为此对象定义无参数的构造函数。”对于使用结构映射的控制器中的IBus - “No parameterless constructor defined for this object.” for IBus in controller using StructureMap 奇怪的错误:没有为此对象定义无参数构造函数 - Strange error: No parameterless constructor defined for this object 在MVC中没有为此对象错误定义无参数构造函数 - No parameterless constructor defined for this object error in MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM