2 回答

TA貢獻1829條經驗 獲得超13個贊
這件事你確實想多了。強類型僅意味著(在這種情況下)您擁有顯式表達自身的類。這實際上只是底層的面向對象編程。
即Segment是一個類,Field是一個類,它具有簡單類型和其他強類型類等的屬性。
如果您需要分段中的更多信息,只需向其中添加更多屬性等即可。
給定
public class HL7Message
{
public List<Segment> Segments { get; set; }
}
public class Segment
{
public string Name { get; set; }
public List<Field> Fields { get; set; }
}
public class Field
{
public string Name { get; set; }
public List<Field> Fields { get; set; }
}
設置
var message = new HL7Message()
{
Segments = new List<Segment>()
{
new Segment()
{
Name = "PID",
Fields = new List<Field>()
{
new Field()
{
Name = "SomeField",
Fields = new List<Field>()
{
new Field()
{
Name = "SomeSubField",
Fields = new List<Field>()
{
new Field()
{
Name = "SomeSubSubField",
}
}
}
}
}
}
}
}
};
用法
var someResult = message.Segments[1].Fields[1].Fields[1];
注意:這并不是要構建您的應用程序,而只是解決您對許多問題的困惑。

TA貢獻1810條經驗 獲得超4個贊
另一種可能且稍微簡潔的方法可能是將其簡化為自引用類或節點模型(即 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", "[email protected]" },
},
};
var child = new HL7Node {
Fields = new Dictionary<string, object> {
{ "fname", "Bob" },
{ "lname", "Doe" },
{ "email", "[email protected]" },
},
};
var grandChild = new HL7Node {
Fields = new Dictionary<string, object> {
{ "fname", "Sally" },
{ "lname", "Doe" },
{ "email", "[email protected]" },
},
};
var greatGrandChild = new HL7Node {
Fields = new Dictionary<string, object> {
{ "fname", "Ray" },
{ "lname", "Doe" },
{ "email", "[email protected]" },
},
};
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)、匿名對象等來執行這些約定。
- 2 回答
- 0 關注
- 141 瀏覽
添加回答
舉報