- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个变量 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/
我想知道是否可以迭代包含 Expando 对象数组的 ExpandoObject? 我目前正在解析一些具有如下文件结构的 JSON: "event": [{ "name": "BEG
我有一个包含 ExpandoObjects 字典的列表。我将它绑定(bind)到网格,但现在我想对列表进行排序。 var rows = new List(); for
我有一个继承的 .NET 4.0 应用程序,它作为 Windows 服务运行。无论如何,我都不是 .NET 专家,但是在编写了 30 多年的代码之后,我知道如何找到自己的出路。 当服务第一次启动时,它
我正在尝试将方法(函数)分配给具有此签名的 ExpandoObject: public List CreateList(string input1, out bool processingStatus
ExpandoObject 是否有方便的工厂方法?有点像,我不知道, dynamic disney = new ExpandoObject("First", "Donald", "Last", "Du
我是 C# 的新手,在 Stackoverflow.com 上遇到了很多问题,但没有找到满足我要求的解决方案。所以最后在这里发帖。 我的要求是从数据表列名创建动态属性,并将值设置为数据表中的动态属性。
这是基于 MVVM 的 WPF 项目: 我在对话框的 View 模型中使用 ExpandoObject,它工作得很好,因为它实现了 INotifyPropertyChanged,我可以直接在 XAML
我正在使用 Json.Net 将 json 结果反序列化为 ExpandoObjects使用如下代码: var converter = new ExpandoObjectConverter()
我正在获取一个 JSON 对象(可能包含多个级别的 JSON 数组等),我想将其转换为 ExpandoObject。 我想出了如何在运行时向 ExpandoObject 添加简单属性,因为它实现了 I
dynamic model = new ExpandoObject(); model.Data = "asdf"; List listOfx = new List(); for (int i = 0;
我真的很喜欢匿名类型语法。例如 new {a = 1, b = 2, c = 25.2} 如果 Expando 或 Dictionary 有这样的版本就好了。有办法吗? 最佳答案 你可以这样做 Di
在我的应用程序中,我必须使用 ExpandoObject 才能在运行时创建/删除属性;但是,我必须将函数返回的 ExpandoObject 映射到相应的对象/类。所以我想出了一个可以完成这项工作但有
有没有办法向 ExpandoObject 添加一个与字符串值同名的属性? 例如,如果我有: string propName = "ProductNumber"; dynamic obj = new S
今天研究 C# 动态时,有些事情让我感到惊讶(我从来没有经常使用它们,但最近我一直在试验 Nancy 网络框架)。我发现我不能这样做: dynamic expando = new ExpandoObj
ExpandoObject添加到 .NET 4 的类允许您在运行时任意设置对象的属性。 与使用 Dictionary 相比,这有什么优势吗? ,甚至是 Hashtable ?据我所知,这不过是一个哈希
我需要创建一个对象,该对象具有动态命名的属性,例如: john max asdf 这可能吗? 最佳答案 是的,绝对是。只需将其用作 IDictionary填充: IDictionary
如果我有这样的 ExpandoObject: dynamic d = new ExpandoObject(); d.x = "a"; d.y = "b"; 并使用 JsonFormatter 将其通过
我写了一个漂亮的函数,它可以接受 system.object ,反射(reflect)其属性并将对象序列化为 JSON 字符串。它看起来像这样: public class JSONSerializer
我正在尝试编写一个通用实用程序,以便从 .NET 外部通过 COM 使用(/skip long story)。无论如何,我正在尝试向 ExpandoObject 添加属性,并且需要将 Property
编辑: ExpandoObject与 MEF 配合良好。我只是有一个语法错误。傻我。 我目前正在使用 ExpandoObjects动态创建 DelegateCommand给我的ViewModels .
我是一名优秀的程序员,十分优秀!