动态典型化列表 json.net,反序列化与反射
typify a list dynamically json.net, deserialization with reflection
我有个小问题。
我应该通过 C# 中的库 json.net 反序列化一个 json。
我不明白你如何切换以粗体输入列表,当然,所有使用反射,因为我正在做一个通用方法。
谁能给我一个启示?或解决方案:)
public static void insertTable(String json, String table)
{
using (localDBService db = new localDBService())
{
dynamic item = Activator.CreateInstance(Type.GetType("eStartService." + table));
item = JsonConvert.DeserializeObject(json);
try
{
var set = db.Set(item.GetType());
set.Add(item);
db.SaveChanges();
log.Info("Scritto sul database dal metodo InsertTable");
}
catch (Exception ex)
{
log.Info("ERRORE DI SCRITTURA SUL DATABASE METODO : insertTable");
throw ex;
}
}
}
错误:实体类型 JArray 不是当前上下文模型的一部分。
谢谢
我的需要是能够读取 json ,在您的对象中动态地反序列化,就像您现在看到的那样……并从我粘贴的那个错误中解脱出来。
任何解决方案?
我们应该根据
这样的特定类型创建List<>
的通用类型
var listType = typeof (List<>).MakeGenericType(type);
这是将 json 值反序列化为具有自定义类型的列表的完整示例:
public static void insertTable(String json, String table)
{
using (localDBService db = new localDBService())
{
var tableType = Type.GetType("eStartService." + table);
var list = DeserializeList(json, tableType);
try
{
var set = db.Set(tableType);
foreach (var item in list)
set.Add(item);
db.SaveChanges();
log.Info("Scritto sul database dal metodo InsertTable");
}
catch (Exception ex)
{
log.Info("ERRORE DI SCRITTURA SUL DATABASE METODO : insertTable");
throw ex;
}
}
}
private static IList DeserializeList( string value,Type type)
{
var listType = typeof (List<>).MakeGenericType(type);
var list = JsonConvert.DeserializeObject(value, listType);
return list as IList;
}
我有个小问题。 我应该通过 C# 中的库 json.net 反序列化一个 json。 我不明白你如何切换以粗体输入列表,当然,所有使用反射,因为我正在做一个通用方法。
谁能给我一个启示?或解决方案:)
public static void insertTable(String json, String table)
{
using (localDBService db = new localDBService())
{
dynamic item = Activator.CreateInstance(Type.GetType("eStartService." + table));
item = JsonConvert.DeserializeObject(json);
try
{
var set = db.Set(item.GetType());
set.Add(item);
db.SaveChanges();
log.Info("Scritto sul database dal metodo InsertTable");
}
catch (Exception ex)
{
log.Info("ERRORE DI SCRITTURA SUL DATABASE METODO : insertTable");
throw ex;
}
}
}
错误:实体类型 JArray 不是当前上下文模型的一部分。
谢谢
我的需要是能够读取 json ,在您的对象中动态地反序列化,就像您现在看到的那样……并从我粘贴的那个错误中解脱出来。 任何解决方案?
我们应该根据
这样的特定类型创建List<>
的通用类型
var listType = typeof (List<>).MakeGenericType(type);
这是将 json 值反序列化为具有自定义类型的列表的完整示例:
public static void insertTable(String json, String table)
{
using (localDBService db = new localDBService())
{
var tableType = Type.GetType("eStartService." + table);
var list = DeserializeList(json, tableType);
try
{
var set = db.Set(tableType);
foreach (var item in list)
set.Add(item);
db.SaveChanges();
log.Info("Scritto sul database dal metodo InsertTable");
}
catch (Exception ex)
{
log.Info("ERRORE DI SCRITTURA SUL DATABASE METODO : insertTable");
throw ex;
}
}
}
private static IList DeserializeList( string value,Type type)
{
var listType = typeof (List<>).MakeGenericType(type);
var list = JsonConvert.DeserializeObject(value, listType);
return list as IList;
}