继承自泛型字典的例子
这段代码定义了一个多层嵌套的字典结构,旨在组织和存储复杂的层级数据
using system; using system.threading.tasks; class contract : dictionary<string, dictionary<string, dictionary<string, string>>> { private readonly string type = "autodesk.data:exchange.contract.dynamo-1.0.0"; public contract() { var contractcontent = new dictionary<string, dictionary<string, string>> { { "contract", new dictionary<string, string>() } }; add(type, contractcontent); } } class program { static void main() { // create an instance of contract var contract = new contract(); // access the outer dictionary using the predefined type key var typekey = "autodesk.data:exchange.contract.dynamo-1.0.0"; // check if the key exists and add data if (contract.trygetvalue(typekey, out var contractcontent)) { // access the inner dictionary with the key "contract" if (contractcontent.trygetvalue("contract", out var innerdictionary)) { // add key-value pairs to the innermost dictionary innerdictionary["key1"] = "value1"; innerdictionary["key2"] = "value2"; // retrieve and display values console.writeline(innerdictionary["key1"]); // outputs: value1 console.writeline(innerdictionary["key2"]); // outputs: value2 } } } }
value1 value2
再看一个dynamo项目实例,这段代码的目的是创建一个嵌套字典结构,用于存储有关二进制引用组件的属性数据。使用接口ipropertyset作为多态机制的基础。通过嵌套字典结构实现多层次数据的组织和访问。提供灵活的属性管理,适合复杂对象的属性存储场景
using system; using system.threading.tasks; class binaryreferencecomponent : dictionary<string, dictionary<string, ipropertyset>> { private string objectid = "autodesk.data:binary.reference.component-1.0.0"; public string objectid { get { return objectid; } } public binaryreferencecomponent(string binaryid) { var propertydictionary = new dictionary<string, ipropertyset>(); propertydictionary.add("string", new stringpropertyset(binaryid)); propertydictionary.add("uint32", new intpropertyset()); this.add("binary_reference", propertydictionary); } } class stringpropertyset : ipropertyset { public string id { get; set; } public string revision { get; set; } public stringpropertyset(string binaryid, string revision = "v0") { id = binaryid; revision = revision; } } class intpropertyset : ipropertyset { public int end { get; set; } public int start { get; set; } public intpropertyset(int start = 0, int end = 8710) { end = end; start = start; } } interface ipropertyset { } class program { static void main() { // create an instance of binaryreferencecomponent with a binaryid var binaryreference = new binaryreferencecomponent("examplebinaryid"); // access objectid console.writeline($"objectid: {binaryreference.objectid}"); // access properties of the component if (binaryreference.trygetvalue("binary_reference", out var propertyset)) { if (propertyset.trygetvalue("string", out var stringproperty)) { var stringprop = stringproperty as stringpropertyset; console.writeline($"stringproperty id: {stringprop.id}, revision: {stringprop.revision}"); } if (propertyset.trygetvalue("uint32", out var intproperty)) { var intprop = intproperty as intpropertyset; console.writeline($"intproperty start: {intprop.start}, end: {intprop.end}"); } } } }
objectid: autodesk.data:binary.reference.component-1.0.0 stringproperty id: examplebinaryid, revision: v0 intproperty start: 0, end: 8710
继承多种集合类型
在c#中,除了泛型字典外,你还可以继承其他集合类型,例如:
- list:可以创建自定义列表类,但不常见,建议使用组合
- hashset:用于无重复元素集合,但继承并不常见
- queue和stack:分别用于队列和栈的实现
- collection和readonlycollection:更适合继承,提供了更好的方法定制化能力
示例:继承 collection
代码说明
customcollection<t>
类:- 继承自
collection<t>
- 重写了
insertitem
方法,添加插入前后的自定义行为 - 定义了一个
itemadded
事件,当新项目添加时触发
- 继承自
main
函数:- 创建
customcollection<string>
的实例 - 订阅
itemadded
事件,输出添加的项信息 - 添加几个项目并显示集合中的所有项目
- 创建
关键点
- 事件处理: 使用事件机制通知项的添加
- 自定义行为: 通过重写方法实现特定逻辑
- 灵活性:
customcollection<t>
可以用于任何类型的集合
using system; using system.collections.objectmodel; class customcollection<t> : collection<t> { // event triggered when an item is added public event action<t> itemadded; // custom implementation of insertitem protected override void insertitem(int index, t item) { // add custom behavior before inserting console.writeline($"inserting item at index {index}: {item}"); base.insertitem(index, item); // trigger the event after inserting itemadded?.invoke(item); } // method to display all items public void displayitems() { console.writeline("current items in collection:"); foreach (var item in this) { console.writeline(item); } } } class program { static void main() { // create an instance of customcollection var collection = new customcollection<string>(); // subscribe to the itemadded event collection.itemadded += item => console.writeline($"item added: {item}"); // add items to the collection collection.add("item1"); collection.add("item2"); collection.add("item3"); // display all items in the collection collection.displayitems(); } }
运行结果:
inserting item at index 0: item1
item added: item1
inserting item at index 1: item2
item added: item2
inserting item at index 2: item3
item added: item3
current items in collection:
item1
item2
item3
注意
在c#中,类继承自泛型字典并不常见。以下是一些原因和建议:
原因
- 违背封装原则:直接继承集合类可能导致外部代码直接操作集合内部结构,违背封装原则
- 集合行为的复杂性:集合类提供的行为可能不完全适合自定义类的需求,可能需要重写大量方法。继承集合类可能引入维护复杂性
组合优于继承:常用的设计模式是组合,即在类内部包含一个集合,而不是继承它
建议
- 使用组合:在类中定义一个集合字段,并通过方法或属性操作它。这可以更好地控制访问和行为
扩展方法:如果需要对集合进行特定操作,可以使用扩展方法来增强其功能,而不是继承
将前面的一个继承的例子改为使用组合,运行结果不变
using system; using system.collections.generic; interface ipropertyset { } class stringpropertyset : ipropertyset { public string id { get; set; } public string revision { get; set; } public stringpropertyset(string binaryid, string revision = "v0") { id = binaryid; revision = revision; } } class intpropertyset : ipropertyset { public int end { get; set; } public int start { get; set; } public intpropertyset(int start = 0, int end = 8710) { end = end; start = start; } } class binaryreferencecomponent { private dictionary<string, dictionary<string, ipropertyset>> properties = new(); public string objectid { get; } = "autodesk.data:binary.reference.component-1.0.0"; public binaryreferencecomponent(string binaryid) { var propertydictionary = new dictionary<string, ipropertyset> { { "string", new stringpropertyset(binaryid) }, { "uint32", new intpropertyset() } }; properties.add("binary_reference", propertydictionary); } public dictionary<string, ipropertyset> getproperties(string key) { properties.trygetvalue(key, out var result); return result; } } class program { static void main() { // create an instance of binaryreferencecomponent var binaryreference = new binaryreferencecomponent("examplebinaryid"); // access objectid console.writeline($"objectid: {binaryreference.objectid}"); // retrieve properties using getproperties method var properties = binaryreference.getproperties("binary_reference"); if (properties != null) { if (properties.trygetvalue("string", out var stringproperty)) { var stringprop = stringproperty as stringpropertyset; console.writeline($"stringproperty id: {stringprop.id}, revision: {stringprop.revision}"); } if (properties.trygetvalue("uint32", out var intproperty)) { var intprop = intproperty as intpropertyset; console.writeline($"intproperty start: {intprop.start}, end: {intprop.end}"); } } else { console.writeline("no properties found for the given key."); } } }
参考
到此这篇关于c#类继承自泛型集合的文章就介绍到这了,更多相关c#类继承自泛型集合内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论