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

标签 c# json serialization dynamic

我有一个变量 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/

相关文章:

c# - 将两个包含属性的对象合并为一个对象

c# - 以编程方式调用 C# 编译器来编译 C# 代码

python - 使用 Django 1.7+ 和数据迁移加载初始数据

json - 如何在resteasy 3中正确反序列化日期

c# - 如果元素名称以句点结尾,则发生 BsonSerializationException

php - 处理序列化数据与添加更多字段 - php - mysql

c# - ef代码第一个principal和dependent的区别

c# - 属性注入(inject)和设置注入(inject)类型的属性

javascript - 将 JS 对象字符串化为 JSON(将函数字符串化为属性)

express - JSONAPI 指定的访客和授权用户的序列化器 `User` 模型的最佳实践是什么?