【问题标题】:Need help making a generic function / interface (C#)需要帮助制作通用函数/接口(C#)
【发布时间】:2014-11-22 02:58:02
【问题描述】:

我有一些代码对于几种不同的数据类型重复完全相同。我想将其简化为一种通用方法,但我无法弄清楚如何做到这一点。我之前发过这个,显示了很大一部分实际代码:Exact same code repeated multiple times for different data types; Any way to make one function that can handle all possible types?

这就是我希望我的通用函数做的事情。我知道我写的不正确,但我认为这应该可以说明我的意图:

private List<LinkData> ProcessSections(<T> sections)
{
    if (sections != null)
    {
        foreach (var item in sections)
        {
            tempLD = new LinkData();
            tempLD.Text = item.SectionTitle;
            tempLD.Class = "class=\"sub-parent\"";
            autoData.Add(tempLD);

            if (item.Link != null && item.Link.Length > 0)
            {
                foreach (var child in item.Link)
                {
                    tempLD = new LinkData
                    {
                        Text = child.a.OuterXML,
                        Link = child.a.href,
                        Class = "class=\"\""
                    };
                    autoData.Add(tempLD);
                }
            }
        }
    }
    return autoData;
}

sections 可以是四种可能的数据类型,但所有四种的使用方式完全相同;特定的数据类型仅取决于需要如何反序列化 XML 页面。这些是四种类型:MaintainedPageLeftContentAdditionalSection[]、StandardPageLeftContentAdditionalSection[]、StoryPageLeftContentAdditionalSection[] 和 DoctorPageLeftContentAdditionalSection[]。这里有两个例子,你可以看到它们的功能基本相同。

public partial class MaintainedPageLeftContentAdditionalSection
{

    private string sectionTitleField;

    private MaintainedPageLeftContentAdditionalSectionLink[] linkField;

    /// <remarks/>
    public string SectionTitle
    {
        get
        {
            return this.sectionTitleField;
        }
        set
        {
            this.sectionTitleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Link")]
    public MaintainedPageLeftContentAdditionalSectionLink[] Link
    {
        get
        {
            return this.linkField;
        }
        set
        {
            this.linkField = value;
        }
    }
}


public partial class StandardPageLeftContentAdditionalSection
{

    private string sectionTitleField;

    private StandardPageLeftContentAdditionalSectionLink[] linkField;

    /// <remarks/>
    public string SectionTitle
    {
        get
        {
            return this.sectionTitleField;
        }
        set
        {
            this.sectionTitleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Link")]
    public StandardPageLeftContentAdditionalSectionLink[] Link
    {
        get
        {
            return this.linkField;
        }
        set
        {
            this.linkField = value;
        }
    }
}

那么,如何创建一个可以接受任何 AdditionalContent 类型的通用函数?

【问题讨论】:

  • 每个预期的类型都可以派生自一个公共类/接口吗?

标签: c# generics interface


【解决方案1】:

听起来你应该有一个包含所有重复代码的部分的抽象基类 - 老实说,这并不完全清楚为什么你需要单独的类型。然后您可以:

private List<LinkData> ProcessSections(IEnumerable<SectionBase> sections)

...并使用从 C# 4 开始的通用协方差来知道 List&lt;ConcreteSection&gt;(或其他)仍然实现 IEnumerable&lt;SectionBase&gt;

您还应该研究自动实现的属性,这可以使您的代码大大更短,例如

public partial class StandardPageLeftContentAdditionalSection
{
    public string SectionTitle { get; set; }

    [XmlElement("Link")]
    public StandardPageLeftContentAdditionalSectionLink[] Link { get; set; }
}

(当然,这将是抽象基类的主体 - 然后您的每个具体类都将派生自它。同样,如果您真的需要单独的类型。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2012-08-11
    相关资源
    最近更新 更多