慕莱坞森
另一种可能且稍微简洁的方法可能是将其简化为自引用类或节点模型(即 XML 或 @TheGeneral 在其示例中具有相同的类Field),其中您可以有 sub-sub-sub-sub-sub.. .fields 如果你愿意的话。每个节点都是相同的(即可预测的),具有相同级别的功能支持。注意:下面类中的构造函数确保Children属性始终被初始化,以避免处理空值。using System;using System.Collections.Generic;public class HL7Node{ public IDictionary<string, object> Fields {get; set; } public List<HL7Node> Children { get; set; } public HL7Node() { Children = new List<HL7Node>(); }}示例用法(另请参阅https://dotnetfiddle.net/EAh9iu):var root = new HL7Node { Fields = new Dictionary<string, object> { { "fname", "John" }, { "lname", "Doe" }, { "email", "jdoe@example.com" }, },};var child = new HL7Node { Fields = new Dictionary<string, object> { { "fname", "Bob" }, { "lname", "Doe" }, { "email", "bdoe@example.com" }, },};var grandChild = new HL7Node { Fields = new Dictionary<string, object> { { "fname", "Sally" }, { "lname", "Doe" }, { "email", "sdoe@example.com" }, },};var greatGrandChild = new HL7Node { Fields = new Dictionary<string, object> { { "fname", "Ray" }, { "lname", "Doe" }, { "email", "rdoe@example.com" }, },};root.Children.Add(child);root.Children[0].Children.Add(grandChild);root.Children[0].Children[0].Children.Add(greatGrandChild);var message = string.Format("Grandchild's name is {0}", root.Children[0].Children[0].Fields["fname"]);我不知道 HL7 消息交换的命名约定要求是什么,但也许仍然有机会使用序列化装饰器(即Newtonsoft.Json.JsonPropertyAttribute)、匿名对象等来执行这些约定。