另一种可能且稍微干净的方法可能是将其简化为自引用类或节点模型(即 XML 或相同的 Field 类 @TheGeneral 在他们的示例中),您可以在其中拥有 sub-sub-sub -sub-sub...字段,如果你愿意的话。然后每个节点都是相同的(即可预测的),具有相同级别的功能支持。
注意:下面类中的构造函数确保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)、匿名对象等来执行那些约定。