对于不同的数据类型,完全相同的代码重复多次;有什么方法可以创建一个可以处理所有可能类型的函数吗?

时间:2014-11-21 17:12:44

标签: c# function types

我正在尝试优化其他人编写的代码。在一节中,它有很多重复的代码;有四个'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和部分是StandardPageLeftContentAdditionalSections;在第二个pageData中是DoctorPage,部分是DoctorPageLeftContentAdditionalSections;等...

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

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;
}

考虑到每次都是不同数据类型的问题,有什么办法可以简化这个吗?

1 个答案:

答案 0 :(得分:1)

使用界面:

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);
            }
        }
    }
}