我需要将一些 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 逻辑的合适逻辑。