gpt4 book ai didi

c# - 如何将 List 中的每个对象转换为自己的类型?

转载 作者:行者123 更新时间:2023-11-30 16:54:59 27 4
gpt4 key购买 nike

我有一个变量 inputs 定义为 List<ExpandoObject>

这是jsonList反序列化的结果,jsonList是不同结构对象的JSON数组:

dynamic inputs = JsonConvert.DeserializeObject<List<ExpandoObject>>(jsonList, converter);

遍历它们我可以获得每个对象的目标类型,因为它们都包含一个属性 Type 有一个 ClassName 一个目标对象。

foreach (dynamic input in inputs)
{
// Inside that loop I can get the type
var inputType = Type.GetType(string.Format("WCFService.{0}", input.Type));
// WCFService is a namespace
// How can I convert here dynamic **input** object
// into an object of type inputType ??
}

基本上,我想在该循环内将 input 对象转换为在 input.Type 中指定为字符串的相应类型

感谢任何帮助。

编辑

在 for-each 循环中我想做这样的事情:

var json = JsonConvert.SerializeObject(input);
Type T = Type.GetType(string.Format("WCFService.{0}", input.Type));
T obj = JsonConvert.DeserializeObject<typeof(T)>(json); // this line fails compilation

这样 obj 将是一个强类型对象。我使用 json 序列化来反序列化,这样所有 json 属性都会自动复制到强类型的 obj 中。但是上面的代码没有编译,最后一行在 T 上提示:

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

EDIT2

仅供引用,传入的 jsonList 具有此结构,该数组中的每个对象都可以具有不同的属性,名称和类型除外:

[
{
"Name": "PLAN-A",
"Type": "CalcInputTypes1",
"CS": 1.1111,
"CUSTOM_DATE1": "2015-05-22",
"CUSTOM_EARN1": 65500.0,
"GENDER": "Male"
},
{
"Name": "PLAN-B",
"Type": "CalcInputTypes2",
"CS": 2.22222,
"CUSTOM_DATE2": "2015-05-23",
"CUSTOM_EARN2": 12000.0,
"PROVINCE": "Ontario"
}
]

CalcInputTypes1、CalcInputTypes2 和最有可能的 CalcInputTypes3、4、5... 是该数组中此类对象的类型...

解决方案

感谢大家的帮助,特别是建议使用 JObject 而不是 ExpandoObject,这使得解决方案变得更加容易和简单:注意:“thing”永远不会起作用,因为在这种情况下 T 必须在编译时已知,但我需要在运行时确定类型,所以解决方案将是这样的:

    public CalcOutputTypes Calculate2(string jsonList)
{
var jobjects = JsonConvert.DeserializeObject<List<JObject>>(jsonList);

foreach (var jobject in jobjects)
{
Type runtimeType = Type.GetType(string.Format("WCFService.{0}", jobject.GetValue("TYPE")));

var input = jobject.ToObject(runtimeType); // Here we convert JObject to the defined type that just created runtime

// At this moment you have a strongly typed object "input" (CalcInputTypes1 or CalcInputTypes2 or...)

}

return new CalcOutputTypes() { STATUS = "Everything is OK !! (input was: json array of heterogeneous objects)" }; // HERE YOU RETURN CalcOutputTypes OBJECT
}

最佳答案

您可以避免使用 ExpandoObject 而是使用 LINQ to JSON直接,像这样:

        var query = from obj in JsonConvert.DeserializeObject<List<JObject>>(jsonList, converter)
let jType = obj["Type"]
where jType != null
let type = Type.GetType(string.Format("WCFService.{0}", (string)jType))
where type != null
where obj.Remove("Type") // Assuming this is a synthetic property added during serialization that you want to remove.
select obj.ToObject(type);
var objs = query.ToList();

如果您需要将该转换器传递给每个特定的ToObject() 调用,您可以执行以下操作:

        var settings = new JsonSerializerSettings();
settings.Converters.Add(converter);
var serializer = JsonSerializer.Create(settings);

var query = from obj in JsonConvert.DeserializeObject<List<JObject>>(jsonList, settings)
let jType = obj["Type"]
where jType != null
let type = Type.GetType(string.Format("WCFService.{0}", (string)jType))
where type != null
where obj.Remove("Type") // Assuming this is a synthetic property added during serialization that you want to remove.
select obj.ToObject(type, serializer);
var objs = query.ToList();

关于c# - 如何将 List<ExpandoObject> 中的每个对象转换为自己的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060974/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com