[英]C# strongly typed properties
I posted a question but need it clarified.我发布了一个问题,但需要澄清。 I'm a beginner.我是初学者。
I'm not sure what the use of the term "strongly typed properties" means in this context?我不确定在这种情况下使用“强类型属性”一词是什么意思? The syntax offered by the responder to my original post is what I'm after, but when I web search for this term, it only offers a definition and examples, not useful examples on how it's implemented in user defined classes. 响应者为我的原始帖子提供的语法正是我所追求的,但是当我在网络上搜索这个术语时,它只提供了定义和示例,没有提供有关如何在用户定义的类中实现的有用示例。
Wouldn't this be best served with strongly typed properties, like h.Segments["PID"].Fields[5].Subfields[3].SubSubFields[2]?这不是最好与强类型属性一起使用吗,比如 h.Segments["PID"].Fields[5].Subfields[3].SubSubFields[2]? – Lasse Vågsæther Karlsen Aug 19 at 7:25 – Lasse Vågsæther Karlsen 8 月 19 日 7:25
Overall, my aim is to总的来说,我的目标是
I'd ideally like to create and object like我理想地喜欢创建和对象
HL7Message h = new HL7Message;
string name = h.segment[2].field[5].subfield[0];
How can I create and access an object whose properties have properties themselves?如何创建和访问其属性本身具有属性的对象?
You are really over-thinking this.你真的是想多了。 Strongly typed only means (in this context), that you have classes that express themselves explicitly.强类型仅意味着(在这种情况下),您有明确表达自己的类。 This is really just ground level object oriented programming.这实际上只是底层面向对象的编程。
Ie Segment
is a class , Field
is a class , which have properties to simple types and also other strongly typed classes, etc.即Segment
是一个class , Field
是一个class ,它具有简单类型和其他强类型类的属性等。
If you need more information in segments, just add more properties etc. to it.如果您需要更多的段信息,只需向其中添加更多属性等。
Given给定的
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; }
}
Setup设置
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",
}
}
}
}
}
}
}
}
};
Usage用法
var someResult = message.Segments[1].Fields[1].Fields[1];
Note : This is not trying to build your application, merely just address the confusion you have been having over many questions.注意:这并不是要构建您的应用程序,而只是解决您对许多问题的困惑。
Another possible and slightly cleaner approach to this might be simplifying it to a self-referential class or node model (ie XML or the same Field
class @TheGeneral has in their example) where you could have sub-sub-sub-sub-sub...fields if you wanted to.另一种可能且稍微简洁的方法可能是将其简化为自引用类或节点模型(即 XML 或 @TheGeneral 在他们的示例中具有的相同Field
类),您可以在其中使用 sub-sub-sub-sub-sub。 ..fields 如果你想。 Every node then is identical (ie predictable) with the same level of feature support.每个节点都是相同的(即可预测的),具有相同级别的功能支持。
Note: The constructor in the below class ensures the Children
property is always initialized so as to avoid handling nulls.注意:下面类中的构造函数确保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>();
}
}
Example usage (see also https://dotnetfiddle.net/EAh9iu ):示例用法(另请参见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"]);
I don't know what your naming conventions requirements are for HL7 message exchange, but perhaps there's some opportunity to still execute those with serialization decorators (ie Newtonsoft.Json.JsonPropertyAttribute
), anonymous objects, etc.我不知道您对 HL7 消息交换的命名约定要求是什么,但也许仍有机会使用序列化装饰器(即Newtonsoft.Json.JsonPropertyAttribute
)、匿名对象等来执行那些命名约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.