无法理解VBScript的代码集

本文关键字:代码 VBScript | 更新日期: 2023-09-27 18:12:46

我需要将一些VBScript代码转换为c# 2.0,下面是需要转换为c#的VBScript代码。

' Component Template titles handled by the summary templates
FeaturedCT = "Featured Summary"
SummaryCT = "Summary"
' Set the looping variables on the first component presentation
If IsFirstComponent = 0 Then
    IsFirstComponent = 1
    ' Start out left handed by default
    IsLeftHand = True
    matchCount = 0
    Dim components()
    ReDim components(Page.ComponentPresentations.Count)
    ' Build a list of all the matching component presentations to be rendered out as summaries
    For Each objCP In Page.ComponentPresentations
        ' Is this a Summary component template?
        If objCP.ComponentTemplateTitle = SummaryCT Or objCP.ComponentTemplateTitle = FeaturedCT Then
            ' Check if this object should be included based on its approval status
            If staging Or getFieldValue(objCP.Component.MetadataFields("ApprovalStatus"), "") = "" Then
                If getFieldValue(objCP.Component.MetadataFields("EndDate"), "") <> "" Then
                    If getDateSerial(objCP.Component.MetadataFields("EndDate").Value(1), False) > getDateSerial(Now, False) Then
                        Set components(matchCount) = objCP
                        matchCount = matchCount + 1
                    End If
                Else
                    Set components(matchCount) = objCP
                    matchCount = matchCount + 1
                End If
            End If
        End If
    Next
    ' Resize the array to the amount of matches
    ReDim Preserve components(matchCount)
End If
For i = 0 to UBound(components) - 1
    ' Determine which component to render from the pre-selected array
    If components(i).ComponentID = Component.ID And components(i).OrdinalPosition = ComponentPresentation.OrdinalPosition Then
        ' Featured summary is always left aligned and causes all other items to be right-aligned
        If ComponentPresentation.ComponentTemplateTitle = FeaturedCT Then
            HasFeaturedSummary = 1
            IsLeftHand = True           
        End If
        Call RenderEntry(components, i)
        ' If a featured summary was previously present all following items are right-aligned
        If HasFeaturedSummary = 1 Then
            IsLeftHand = False
        Else
            ' Update the left-handed status
            UpdatePositioning
        End If
        If i = UBound(Components) - 1 Then
            WriteOut "<div class=""clearBoth""></div>"
        End If
    End If
Next

下面是我尝试用c#编写的代码。

  public string RenderSummaryCT()
    {
        string FeaturedCT = "Featured Summary CT";
        string SummaryCT = "Summary CT";
        int IsFirstComponent = 0;
        string result = string.Empty;
        int hasFeaturedSummary = 0;
        Component comp = null;
        bool IsLeftHand = false;
        StringBuilder sbOutput = new StringBuilder();
        List<tc.ComponentPresentation> cmp = new List<tc.ComponentPresentation>();
        if (IsFirstComponent == 0)
        {
            IsFirstComponent = 1;
            IsLeftHand = true;
            //m_Logger.Info("CMP Array-" + cmp.Count);
            foreach (tc.ComponentPresentation objCMP in m_Page.ComponentPresentations)
            {
                if ((objCMP.ComponentTemplate.Title == SummaryCT) || (objCMP.ComponentTemplate.Title == FeaturedCT))
                {
                    comp = objCMP.Component;
                    string approvalStatus = string.Empty;
                    string endDate = string.Empty;
                    if (comp.Metadata != null)
                    {
                        ItemFields compItemfields = new ItemFields(comp.Metadata, comp.MetadataSchema);
                        approvalStatus = th.GetSingleStringValue("ApprovalStatus", compItemfields);
                        endDate = th.GetSingleStringValue("EndDate", compItemfields);
                    }
                    if ((baseutility.GetStagingConstantValue(m_Engine, m_Package)) || (string.IsNullOrEmpty(approvalStatus)))
                    {
                        if (!string.IsNullOrEmpty(endDate))
                        {
                            DateTime eDate = Convert.ToDateTime(baseutility.GetDateSerial(Convert.ToDateTime(endDate), false));
                            DateTime currentDate = Convert.ToDateTime(baseutility.GetDateSerial(DateTime.Now, false));
                            if ((eDate) > (currentDate))
                            {
                                cmp.Add(objCMP);
                            }
                        }
                        else
                        {
                            cmp.Add(objCMP);
                            //m_Logger.Info("2. Adding cmp: " + maxCount.ToString() + "-- " + cmp[maxCount].Component.Title);                              
                        }
                    }
                }
            }
        }
        bool lastFlag = false;
        int cnt = 0;
        int totalLength = cmp.Count;
        foreach (tc.ComponentPresentation cm in cmp)
        {
            m_Logger.Info(cm.Component.Id + "--" + m_Component.Id);
            m_Logger.Info(cnt + "--" + totalLength);
            if (cm.Component.Id == m_Component.Id)
            {
                if (cm.ComponentTemplate.Title == FeaturedCT)
                {
                    m_Logger.Info("inside featured CT");
                    hasFeaturedSummary = 1;
                    IsLeftHand = true;
                }
                sbOutput.Append("" + SummaryBase.SummaryHelper.RenderEntry(cmp, cnt, IsLeftHand, lastFlag));
                m_Logger.Info("IsLeftHand -: " + IsLeftHand.ToString());
                if (hasFeaturedSummary == 1)
                {
                    IsLeftHand = false;
                }
                else
                {
                    //sbOutput.Append("" + SummaryBase.SummaryHelper.UpdatePositioning(IsLeftHand));
                    if (IsLeftHand)
                    {
                        IsLeftHand = false;
                    }
                    else
                    {
                        //m_Logger.Info("UpdatePositioning");
                        sbOutput.Append("<div class='"clearBoth'"></div>");
                        IsLeftHand = true;
                    }
                }
                m_Logger.Info("CMP Title -: " + cm.Component.Title);
                cnt = cnt + 1;
                if (totalLength == cnt)
                {
                    m_Logger.Info("cnt-" + cnt);
                    lastFlag = true;
                }
                if (lastFlag)
                {
                    sbOutput.Append(" <div class='"clearBoth'"></div>");
                }
            }
        }
        return sbOutput.ToString();
    }

我肯定有问题,你能建议合适的逻辑使用上面的VBScript逻辑。

无法理解VBScript的代码集

这不仅仅是"将VBScript转换为。net",您还从基于com的Tridion对象模型转移到。net TOM -其工作方式完全不同。

我的建议是对它采取非常分析的观点:

  1. 分解你的代码,把逻辑写在纸上
  2. 试着理解为什么需要发生,如果它仍然应该发生

这将使您以后重写代码变得容易得多。

我当然会利用这个机会重写它的一部分,使它更有意义-像有IsFirstComponent变量转换为布尔值而不是int,并从你的c#代码中删除所有的HTML(不是你有很多在那里,但下一个人在这段代码可能会想知道,HTML不是由一些汇编"隐藏"在你的服务器的某个地方创建的)。

我稍后会尝试看一下这个VBScript,但我认为在这一点上理解代码的真正作用可能更重要。

老实说,我看不出ordinalPosition做了什么。我的猜测是,当您对每个组件表示调用render时,component和ComponentPresentation的当前值被更新。(在旧式模板中,组件和页面共享相同的呈现堆栈。)但我在这里猜测,我仍然不确定意图的逻辑是什么。@Nuno -组件集合通常不会在从页面填充后更新。ComponentPresentations在"Extract Components From Page",所以我怀疑黑魔法是否会这么容易移植。

所以Manu -你需要检查这个特性的功能规范是什么。这个想法可能只是为了测试列表中是否有一个Featured Summary组件表示,如果有,将其放在左边,其余的放在右边。(如果是这样的话,那么实现这个目标的逻辑是相当曲折的。)在实践中,您可能必须检查使用此模板的现有页面,查看它们的功能,并承诺仅支持您的端口中的这些页面。

(您可能还想查看工作流代码的用途。也许在您的活动目标上配置最低批准状态将使您能够删除此代码。

我全心全意地支持Nuno最初的建议,利用这个机会重写一些东西。即使在您的时间限制下,除非您理解问题,然后实现代码,否则您永远不会成功地获得正确的逻辑。说真的,像这样不可维护的代码只会在你快到截止日期的时候造成更大的伤害。

在Tridion页面模板中,处理不同种类的组件表示是一个常见的需求。模板风格在复合模板中非常不同,但是如果使用得当,它比这里展示的方法更容易理解。在Tridion实践项目中有一个基类,当您有机会按照习惯方式编写代码而不是逐行移植时,您可能会发现它很有帮助。