c#-属性信息集值列表<;对象>;
属性信息设置值c#-属性信息集值列表<;对象>;,c#,json,mysqldatareader,C#,Json,Mysqldatareader,属性信息设置值列表 在转换列表中显示错误 我试图转换成json,但我做不到 public class PurchaseBill { public DateTime Date { get; set; } public string Type { get; set; } public string BillType { get; set; } public List<PurchaseProduct> Listproduct { get; set; } }
列表
在转换列表中显示错误
我试图转换成json,但我做不到
public class PurchaseBill
{
public DateTime Date { get; set; }
public string Type { get; set; }
public string BillType { get; set; }
public List<PurchaseProduct> Listproduct { get; set; }
}
public class PurchaseProduct
{
public Int64 id { get; set; }
public String itemId { get; set; }
public String image { get; set; }
}
请我想要解决方案请格式化您的帖子,并包括预计的产出;还有,确切的问题是什么?调试并查看您得到的值是否与预期值相同。数据读取器将不包含包含列表的列。是否要改为分配反序列化对象公司
?显示错误调试mscorlib.dll中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理其他信息:从“System.String”到“System.Collections.Generic.List”的无效转换“1”[[test.Purchase\u Management+PurchaseProduct,App,Version=1.0.0,Culture=neutral,PublicKeyToken=null]'。您能不能请您的问题包括getval
的字符串值?我理解了将类型“company”转换为“prop.PropertyType”的问题
public static List<T> DataReaderMapToList<T>(MySqlDataReader dr)
{
List<T> list = new List<T>();
T obj = default(T);
while (dr.Read())
{
obj = Activator.CreateInstance<T>();
foreach (PropertyInfo prop in obj.GetType().GetProperties())
{
if (!object.Equals(dr[prop.Name], DBNull.Value))
{
if (prop.PropertyType.Name == "List`1")
{
var getval = Convert.ChangeType(dr[prop.Name], typeof(string)).ToString();
var objectType = prop.PropertyType.GetGenericArguments().First();
var Object = Activator.CreateInstance(objectType);
var company = JsonConvert.DeserializeObject<List<object>>(getval);
// error in next line 'can`t convert to list`1 '
prop.SetValue(obj, Convert.ChangeType(dr[prop.Name], prop.PropertyType), null);
}
else
{
prop.SetValue(obj, Convert.ChangeType(dr[prop.Name], prop.PropertyType), null);
}
}
}
list.Add(obj);
}
return list;
}
mysql MySqlCommand cmd = new MySqlCommand(query, connection); //Create a data reader and Execute the command MySqlDataReader dataReader = cmd.ExecuteReader(); //Read the data and store them in the list list = DataReaderMapToList<T>(dataReader);
show error debug An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code Additional information: Invalid cast from 'System.String' to 'System.Collections.Generic.List`1[[test.Purchase_Management+PurchaseProduct, App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.