完全相同的代码针对不同的数据类型重复多次;任何方法都可以使一个可以处理所有可能类型的函数

本文关键字:一个 可以使 处理 类型 函数 有可能 任何 代码 数据类型 方法 | 更新日期: 2023-09-27 18:31:20

我正在尝试优化其他人编写的一些代码。在一个部分中,它有很多重复的代码;有四个"if"语句,其中一个语句中,第一行之后是完全相同的代码。所有不同"if"语句的原因是,根据用户所在的页面类型,数据反序列化的方式不同;但是,在数据反序列化后,每次都完全相同。

if (smartFormId == EktronSmartForms.StandardPage)
{
    var pageData = (EkXml.Deserialize(typeof(StandardPage), ContentData.Html) as StandardPage);
    var sections = pageData.LeftContent.AdditionalSection;
    title = pageData.LeftContent.ParentBreadcrumbTitle;
    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.Count() > 0)
            {
                foreach (var child in item.Link)
                {
                    tempLD = new LinkData();
                    tempLD.Text = child.a.OuterXML;
                    tempLD.Link = child.a.href;
                    tempLD.Class = "class='"'"";
                    autoData.Add(tempLD);
                }
            }
        }
    }
}
else if (smartFormId == EktronSmartForms.DoctorPage)
{
    var pageData =
        (EkXml.Deserialize(typeof(DoctorProfilePage), ContentData.Html) as DoctorProfilePage);
    var sections = pageData.LeftContent.AdditionalSection;
    title = pageData.LeftContent.ParentBreadcrumbTitle;
    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);
                }
            }
        }
    }
}
else if (smartFormId == EktronSmartForms.StoryPage)
{
    var pageData = (EkXml.Deserialize(typeof(StoryPage), ContentData.Html) as StoryPage);
    var sections = pageData.LeftContent.AdditionalSection;
    title = pageData.LeftContent.ParentBreadcrumbTitle;
    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);
                }
            }
        }
    }
}
else if (smartFormId == EktronSmartForms.MaintainedPage)
{
    var pageData =
        (EkXml.Deserialize(typeof(MaintainedPage), ContentData.Html) as MaintainedPage);
    var sections = pageData.LeftContent.AdditionalSection;
    title = pageData.LeftContent.ParentBreadcrumbTitle;
    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);
                }
            }
        }
    }
}

但是,由于 pageData 的创建方式,每个 if 语句都有不同的类型;第一个是 StandardPage,部分是 StandardPageLeftContentAdditionalSection;在第二个页面中,Data 是 DoctorPage,部分是 DoctorPageLeftContentAdditionalSection;等等......

我想创建一个函数,我可以在其中传输所有重复的代码,并在每个"if"语句中调用该函数(或者更好的是,在所有"if"语句的末尾),但是 (1) 我不能在 if 语句之前声明 pageData 和部分,因为如果我声明

var sections = new StandardPageLeftContentAdditionalSections();

如果我尝试这样做,我会得到转换错误

pageData = (EkXml.Deserialize(typeof(DoctorProfilePage), ContentData.Html) as DoctorProfilePage);
sections = pageData.LeftContent.AdditionalSection; 

我想做的是:

var sections = ????
if (smartFormId == EktronSmartForms.StandardPage){
    var pageData = (EkXml.Deserialize(typeof(StandardPage), ContentData.Html) as StandardPage);
    sections = pageData.LeftContent.AdditionalSection;
}
else if (smartFormId == EktronSmartForms.DoctorPage)
{
    var pageData =
        (EkXml.Deserialize(typeof(DoctorProfilePage), ContentData.Html) as DoctorProfilePage);
    sections = pageData.LeftContent.AdditionalSection;
}
etc...
autoData = ProcessSections(type??? sections, List<LinkData> autoData);
________________________________________________________________________________
private List<Link> ProcessSections(type??? sections, List<LinkData> autoData){
    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;
}

鉴于部分每次都是不同数据类型的问题,有什么方法可以简化它吗?

完全相同的代码针对不同的数据类型重复多次;任何方法都可以使一个可以处理所有可能类型的函数

使用接口:

public interface IPageData{
    object LeftContent{get;} //Use the correct return type rather than 'object' here
}

然后,当您声明页面类型时,请确保实现该接口:

public class StandardPage:IPageData {
    ...
}
public class DoctorPage:IPageData{
    ...
}

然后你可以反序列化为一个公共对象:

IPageData pageData;
switch(smartFormId) {
    case EktronSmartForms.StandardPage:
        pageData = (EkXml.Deserialize(typeof(StandardPage), ContentData.Html) as StandardPage);
        break;
    ...
}
var sections = pageData.LeftContent.AdditionalSection;
title = pageData.LeftContent.ParentBreadcrumbTitle;
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.Count() > 0)
        {
            foreach (var child in item.Link)
            {
                tempLD = new LinkData();
                tempLD.Text = child.a.OuterXML;
                tempLD.Link = child.a.href;
                tempLD.Class = "class='"'"";
                autoData.Add(tempLD);
            }
        }
    }
}