首页 > 编程语言 >C#类继承自泛型集合

C#类继承自泛型集合

时间:2024-08-09 17:25:13浏览次数:9  
标签:Console string 自泛 C# var item 集合 new public

继承自泛型字典的例子

这段代码定义了一个多层嵌套的字典结构,旨在组织和存储复杂的层级数据

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

代码说明

  1. CustomCollection<T>:

    • 继承自Collection<T>
    • 重写了InsertItem方法,添加插入前后的自定义行为
    • 定义了一个ItemAdded事件,当新项目添加时触发
  2. 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.");
        }
    }
}

参考

  • https://github.com/DynamoDS/Dynamo.git

标签:Console,string,自泛,C#,var,item,集合,new,public
From: https://blog.csdn.net/mengningyun6826/article/details/141031498

相关文章

  • C语言---指针的运算和各种类型的指针
    指针的运算1.指针+1或者指针-1是什么意思?把指针中记录的内存地址,往后或者往前移动一个步长2.什么是步长?跟什么有关?跟数据类型有关Windows64位操作系统:char:移动一个字节short:移动两个字节int:移动四个字节long:移动四个字节longlong:移动八个字节有意义的操作......
  • HTML静态网页作业(HTML+CSS)——外卖平台主题网页设计制作(8个页面)
    ......
  • AI编程工具FittenCode简直是懒人神器啊!
    AI编程简直太好用了,简直是懒人神器啊。自动帮我检查代码、排查错误、给出多种解决方案。快速且精准,教练级的教授知识、保姆级的贴身服务,而且免费、可以不眠不休。上一篇博文,介绍了VisualStudio如何安装AI编程工具FittenCode.今天,介绍一下FittenCode的具体应用。1.打开Fitt......
  • CLAM实现指定区域分割
    CLAMhttps://github.com/mahmoodlab/CLAM    CLAM(Clustering-constrainedAttentionMultipleInstanceLearning)。旨在用于数据高效的弱监督计算病理学,特别是使用切片级标签对全切片图像(WSI)进行分类,无需提取感兴趣区域 (ROI)或进行切片级别的标注。     先......
  • 聚焦IOC容器刷新环节postProcessBeanFactory(BeanFactory后置处理)专项
    目录一、IOC容器的刷新环节快速回顾二、postProcessBeanFactory源码展示分析(一)模版方法postProcessBeanFactory(二)AnnotationConfigServletWebServerApplicationContext调用父类的postProcessBeanFactory包扫描注解类注册(三)postProcessBeanFactory主要功能三、调用父......
  • 一文搞懂MES、ERP、SCM、WMS、APS、SCADA、PLM、QMS、CRM、EAM及其关系
    MES、ERP、SCM、WMS、APS、SCADA、PLM、QMS、CRM、EAM各个系统到底是什么意思?今天一文就给大家分享!在企业管理中,各种信息系统扮演着至关重要的角色,它们如同企业的神经系统,确保各个部分高效协同运作。MES(ManufacturingExecutionSystem)制造执行系统,就如同工厂的前线指挥官,它实......
  • Spring Cloud接入Nacos作为配置中心和服务发现
    一、nacos介绍Nacos是DynamicNamingandConfigurationService(动态命名和配置服务)的首字母简称,它是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。Nacos由阿里巴巴开源,致力于帮助用户发现、配置和管理微服务。以下是Nacos的详细介绍:动态服务发现:Nacos......
  • 从STM32CubeMX导入项目到Embedded Studio。(原文题目:Import projects from STM32CubeM
    原文链接https://wiki.segger.com/Import_projects_from_STM32CubeMX_to_Embedded_Studio原文来自于SEGGER的wiki,题目是ImportprojectsfromSTM32CubeMXtoEmbeddedStudio原文最后编辑于2022/2/21.摘要:CubeMX生成项目,导入到EmbeddedStudio,并添加必要的文件软件:STM32C......
  • Datawhale AI夏令营-第四期(AIGC方向)-Task01-可图Kolors-LoRA风格故事挑战赛
    从零入门AI生图原理&实践是Datawhale2024年AI夏令营第四期的学习活动(“AIGC”方向),基于魔搭社区“可图Kolors-LoRA风格故事挑战赛”开展的实践学习。下面将分六部分介绍我的学习&实践情况。一、文生图的历程与基石首先,通过社区提供的学习资料和PPT,对文生图的历程与基石进......
  • OFtutorial02_commandLineArgumentsAndOptions
    OFtutorial2.CargList类如图包含很多函数,常用的addNote(输出字符串),noParallel(去掉基类中的并行选项),addBoolOption,addOption(增加选项)源码#include"fvCFD.H"#argc即argumentcount的缩写,保存程序运行时传递给主函数的参数个数;argv即argumentvector的缩写,保存程序运行......