private void GenerateLoopExpressionParamAspxCode(TemplateElement node, ScopeData scopeData) { if (node.ChildNodes.Count == 2) { if (node.ChildNodes[0].Type == TemplateElementTypes.Variable) { TemplateElement variable = node.ChildNodes[0]; TemplateElement target = node.ChildNodes[1]; StringBuffer codeBody = new StringBuffer(); Type returnType = null; switch (target.Type) { case TemplateElementTypes.Variable: GenerateVariableAspxCode(codeBody, target, out returnType, scopeData); break; case TemplateElementTypes.Function: GenerateFunctionAspxCode(codeBody, target, out returnType, scopeData); break; } if (returnType != null) { Type enumerType = returnType.GetInterface("IEnumerable`1"); if (enumerType == null) enumerType = returnType.GetInterface("IEnumerator`1"); Type varType = null; if (enumerType != null) varType = enumerType.GetGenericArguments()[0]; else { if (returnType == s_TypeOfStringCollection || returnType.IsSubclassOf(s_TypeOfStringCollection)) varType = typeof(string); } if (varType != null) { string varName = variable.Items[TemplateElement.KEY_NAME] as string; string i = node.Items["i"] as string; if (varName != null) { ScopeData scope = new ScopeData(scopeData); string iVarName = null; if(i != null) iVarName = scope.DeclaringScopeVariable(i, typeof(int)); m_CodeBody += "<%\r\nif(" + codeBody + " != null)\r\n{%>"; if (iVarName != null) m_CodeBody += "<%\r\nint " + iVarName + "=0;\r\n%>"; m_CodeBody += "<%\r\nforeach(" + ReflectionUtil.GetCSharpTypeName(varType) + " " + scope.DeclaringScopeVariable(varName, varType) + " in " + codeBody + "){%>"; GenerateAspxCode(node.Parent, scope); m_CodeBody += "<%"; if (iVarName != null) m_CodeBody += "\r\n" + iVarName + "+=1;"; m_CodeBody += "\r\n}\r\n}"; m_CodeBody += "%>"; } } } } } }
private void GenerateLoopExpressionParam2AspxCode(TemplateElement node, ScopeData scopeData) { if (node.ChildNodes.Count == 2) { TemplateElement from = node.ChildNodes[0]; TemplateElement to = node.ChildNodes[1]; StringBuffer fromCode = new StringBuffer(); Type fromType = null; switch (from.Type) { case TemplateElementTypes.Variable: GenerateVariableAspxCode(fromCode, from, out fromType, scopeData); break; case TemplateElementTypes.Function: GenerateFunctionAspxCode(fromCode, from, out fromType, scopeData); break; case TemplateElementTypes.Literal: string fromValue = from.SourceTemplate.Substring(from.Index, from.Length); int temp = -1; if (int.TryParse(fromValue, out temp)) { fromCode += fromValue; fromType = typeof(int); } break; } StringBuffer toCode = new StringBuffer(); Type toType = null; switch (to.Type) { case TemplateElementTypes.Variable: GenerateVariableAspxCode(toCode, to, out toType, scopeData); break; case TemplateElementTypes.Function: GenerateFunctionAspxCode(toCode, to, out toType, scopeData); break; case TemplateElementTypes.Literal: string toValue = to.SourceTemplate.Substring(to.Index, to.Length); int temp = -1; if (int.TryParse(toValue, out temp)) { toCode += toValue; toType = typeof(int); } break; } if (fromType == typeof(int) && toType == typeof(int)) { string i = node.Items["i"] as string; string step = node.Items["step"] as string; uint temp = 0; if (i != null && (step == null || (uint.TryParse(step, out temp) && temp > 0))) { if (step == null) step = "1"; ScopeData scope = new ScopeData(scopeData); string iVarName = scope.DeclaringScopeVariable(i, typeof(int)); //example: for (int i = a; a > b ? i >= b : i <= b; i += a > b ? -c : c) m_CodeBody += "<%\r\nfor(int " + iVarName + "=" + fromCode + ";" + fromCode + " > " + toCode + " ? " + iVarName + " >= " + toCode + " : " + iVarName + " <= " + toCode + ";" + iVarName + " += " + fromCode + " > " + toCode + " ? -" + step + " : " + step + "){%>"; GenerateAspxCode(node.Parent, scope); m_CodeBody += "<%\r\n}%>"; } } } }
private void GenerateTagAspxCode(TemplateElement tag, ScopeData scopeData) { string name = ((string)tag.Items[TemplateElement.KEY_NAME]); if (string.Compare(name, "page", true) == 0) return; else if (string.Compare(name, "break", true) == 0) { m_CodeBody += "<% break; %>"; return; } else if (string.Compare(name, "continue", true) == 0) { m_CodeBody += "<% continue; %>"; return; } MethodInfo method = SearchMethodForTag(tag); if (method != null) { string instanceName = DeclaringVariable(method.DeclaringType); m_CodeBody += "<%\r\n" + instanceName + "." + method.Name + "("; int templateIndex = -1; //模板委托类型参数的起始索引 int templateCount = 0; //模板委托类型参数的个数 bool needRemoveDot = false; ParameterInfo[] parameters = method.GetParameters(); #region 将模板标签属性输出为方法参数 for (int i = 0; i < parameters.Length; i++) { if (parameters[i].ParameterType.IsSubclassOf(typeof(Delegate)) == false) { TemplateElement attributeListItem = GetAttributeListItem(parameters[i], tag.ChildNodes[0]); if (attributeListItem != null) { GenerateAttributeListItemAspxCode(parameters[i], attributeListItem, scopeData); } m_CodeBody += ","; needRemoveDot = true; } else { if (templateIndex == -1) templateIndex = i; templateCount++; } } if (needRemoveDot == true) m_CodeBody.InnerBuilder.Remove(m_CodeBody.InnerBuilder.Length - 1, 1); #endregion #region 将模板标签的模板内容输出为匿名委托 for (int i = templateIndex; i - templateIndex < templateCount; i++) { ScopeData scope = new ScopeData(scopeData); if (needRemoveDot == true) { m_CodeBody += ","; } else { needRemoveDot = true; } m_CodeBody += "delegate("; ParameterInfo[] args = parameters[i].ParameterType.GetMethod("Invoke").GetParameters(); for (int j = 0; j < args.Length; j++) { m_CodeBody += ReflectionUtil.GetCSharpTypeName(args[j].ParameterType) + " " + scope.DeclaringScopeVariable(args[j]); if (j < args.Length - 1) m_CodeBody += ","; } m_CodeBody += "){\r\n%>"; if (templateCount == 1) { GenerateAspxCode(tag, scope); } else { for (int j = 1; j < tag.ChildNodes.Count; j++) { TemplateElement element = tag.ChildNodes[j]; if (element.Type == TemplateElementTypes.Tag) { string tagName = (string)element.Items[TemplateElement.KEY_NAME]; if (StringUtil.EqualsIgnoreCase(parameters[i].Name, tagName)) { GenerateAspxCode(element, scope); break; } } } } m_CodeBody += "<%\r\n}"; //for (int j = 0; j < args.Length; j++) //{ // UndeclaringScopeVariable(args[j]); //} } #endregion m_CodeBody += ");%>"; } else { //未能找到合适的注册标签,直接输出 //if (tag.Index != tag.Length) // m_CodeBody += tag.SourceTemplate.Substring(tag.Index, tag.Length); //string name = tag.Items[TemplateElement.KEY_NAME] as string; //if (StringUtil.EqualsIgnoreCase(name, "ajaxpanel")) //{ //} } }