代码片段:自定义模版,模版解析片段
香水坏坏 发表于 2007-7-7 [ASP.NET]
作者: 香水坏坏
转载请著名,谢谢!
主要通过反射、字符串查找替换实现,可应用于电子邮件模版或者其他方面需要用用户自定义模版的地方,但又不能以PAGE形式输出等等
C#代码
- public string AnalyseThemeObj(string val, string name, object obj, Type objType)
- {
- string beginSplit = "<%=", endSplit = "%>";
- int beginPos = val.IndexOf(beginSplit);
- int endPos = 0;
- int lastAnaylsePos = 0;
- StringBuilder ret = new StringBuilder();
- while (beginPos > -1)
- {
- beginPos += beginSplit.Length;
- endPos = val.IndexOf(endSplit, beginPos);
- if (endPos > -1)
- {
- if (endPos - beginPos == 0)
- {
- ret.Append(val.Substring(lastAnaylsePos, beginPos - beginSplit.Length - lastAnaylsePos));
- lastAnaylsePos = endPos + endSplit.Length;
- }
- else if (endPos - beginPos == name.Length)
- {
- ret.Append(val.Substring(lastAnaylsePos, beginPos - beginSplit.Length - lastAnaylsePos));
- ret.Append(obj.ToString());
- lastAnaylsePos = endPos + endSplit.Length;
- }
- else if (endPos - beginPos > name.Length)
- {
- string objStr = val.Substring(beginPos, endPos - beginPos);
- string replaceValue = ReturnObjStringValue(objStr, obj, objType);
- ret.Append(val.Substring(lastAnaylsePos, beginPos - beginSplit.Length - lastAnaylsePos));
- ret.Append(replaceValue);
- lastAnaylsePos = endPos + endSplit.Length;
- }
- }
- beginPos = val.IndexOf(beginSplit, beginPos);
- }
- ret.Append(val.Substring( lastAnaylsePos));
- return ret.ToString();
- }
- public string ReturnObjStringValue(string inputStr, object obj, Type type)
- {
- char objDenotation = ‘.’;
- char[] methodDenotation = { ‘(’, ‘)’ };
- string ret = string.Empty;
- int findPos = 0;
- findPos = inputStr.IndexOf(objDenotation, findPos);
- if (findPos > 0)
- {
- int lastFindPos = -1;
- int checkMenthodBound = inputStr.IndexOf( methodDenotation[0] , findPos+1 );
- if (checkMenthodBound > -1)
- {
- lastFindPos = inputStr.IndexOf(objDenotation, findPos + 1, checkMenthodBound - findPos - 1);
- }
- else
- {
- lastFindPos = inputStr.IndexOf(objDenotation, findPos + 1);
- }
- if (lastFindPos == 0)
- {
- //do nothing , actully should be throw a error with syntax error
- }
- else if (lastFindPos > 0)
- {
- string objStr = inputStr.Substring(findPos + 1, lastFindPos - findPos - 1);
- object tmpObj = ReturnObjFromPropertyOrMethod(objStr, obj, type);
- if (null != tmpObj)
- {
- return ReturnObjStringValue(inputStr.Substring(findPos + 1), tmpObj, tmpObj.GetType());
- }
- else
- {
- //do nothing , actually should be throw a error with null with object
- }
- }
- else
- {
- object tmpObj = ReturnObjFromPropertyOrMethod(inputStr.Substring(findPos + 1), obj, type);
- if (null != tmpObj)
- ret = tmpObj.ToString();
- }
- }
- else
- {
- //do nothing , actually should be throw a error with syntax error
- }
- return ret;
- }
- public object ReturnObjFromPropertyOrMethod(string inputStr, object obj, Type type)
- {
- char[] methodDenotation = { ‘(’, ‘)’ };
- char splitDenotation = ‘,’;
- char stringDenotaion = ‘”‘;
- char mathDenotation = ‘.’;
- object ret = null;
- //analyse the object is a can reader property or public method
- int methodPos = inputStr.IndexOf(methodDenotation[0], 0);
- if (methodPos == 0)
- {
- //do nothing , actully should be throw a error with syntax error
- }
- else if (methodPos > 0)
- {
- //ensure there is a method
- int methodPosEnd = inputStr.IndexOf(methodDenotation[1], methodPos + 1);
- if (methodPosEnd > -1)
- {
- string methodName = inputStr.Substring(0, methodPos );
- MethodInfo method = type.GetMethod(methodName);
- if (null == method || !method.IsPublic)
- {
- }
- else
- {
- if (methodPosEnd - methodPos == 1)
- {
- ret = method.Invoke(obj, null);
- }
- else
- {
- //nowing we are only support null,int,float,bool,string type input
- string methodParamStr = inputStr.Substring(methodPos + 1, methodPosEnd - methodPos - 1);
- string[] paramStrArr = methodParamStr.Split(splitDenotation);
- object[] paramsObj = new object[paramStrArr.Length];
- for (int i = 0; i < paramStrArr.Length; i++)
- {
- if (paramStrArr[i][0] == stringDenotaion )
- {
- if ( paramStrArr[i][paramStrArr[i].Length - 1] == stringDenotaion )
- paramsObj[i] = paramStrArr[i].Substring( 1 , paramStrArr[i].Length - 2 );
- else
- paramsObj[i] = null;
- }
- else if (paramStrArr[i] == “true” || paramStrArr[i] == “false”)
- {
- paramsObj[i] = bool.Parse(paramStrArr[i]);
- }
- else if (paramStrArr[i] == “null”)
- {
- paramsObj[i] = null;
- }
- else
- {
- try
- {
- if (paramStrArr[i].IndexOf(mathDenotation) > -1)
- paramsObj[i] = float.Parse(paramStrArr[i]);
- else
- paramsObj[i] = int.Parse(paramStrArr[i]);
- }
- catch
- {
- paramsObj[i] = null;
- }
- }
- }
- ret = method.Invoke(obj, paramsObj);
- }
- }
- }
- else
- {
- //do nothing , actully should be throw a error with syntax error
- }
- }
- else
- {
- //ensure there is a property
- PropertyInfo property = type.GetProperty(inputStr);
- if (null == property || !property.CanRead)
- {
- //do nothing , becasue there no matching Object Property to return
- }
- else
- {
- object tmpObj = property.GetValue(obj, null);
- if (null != tmpObj)
- {
- ret = tmpObj;
- }
- else
- {
- //do nothing , actually should be throw a error with null with object
- }
- }
- }
- return ret;
- }
访客评论
发表评论
- 你的姓名:
- 你的网站:
- EMAIL:
- 评论内容:
- 私人

