代码片段:自定义模版,模版解析片段

香水坏坏 发表于 2007-7-7 [ASP.NET]

作者: 香水坏坏

转载请著名,谢谢!

主要通过反射、字符串查找替换实现,可应用于电子邮件模版或者其他方面需要用用户自定义模版的地方,但又不能以PAGE形式输出等等

 

C#代码
  1. public  string AnalyseThemeObj(string val, string name, object obj, Type objType)   
  2.         {   
  3.             string beginSplit = "<%=", endSplit = "%>";   
  4.   
  5.             int beginPos = val.IndexOf(beginSplit);   
  6.             int endPos = 0;   
  7.   
  8.             int lastAnaylsePos = 0;   
  9.   
  10.             StringBuilder ret = new StringBuilder();   
  11.   
  12.             while (beginPos > -1)   
  13.             {   
  14.                 beginPos += beginSplit.Length;   
  15.                 endPos = val.IndexOf(endSplit, beginPos);   
  16.                 if (endPos > -1)   
  17.                 {   
  18.                     if (endPos - beginPos == 0)   
  19.                     {   
  20.                         ret.Append(val.Substring(lastAnaylsePos, beginPos - beginSplit.Length - lastAnaylsePos));   
  21.                         lastAnaylsePos = endPos + endSplit.Length;   
  22.                     }   
  23.                     else if (endPos - beginPos == name.Length)   
  24.                     {   
  25.                         ret.Append(val.Substring(lastAnaylsePos, beginPos - beginSplit.Length - lastAnaylsePos));   
  26.                         ret.Append(obj.ToString());   
  27.                         lastAnaylsePos = endPos + endSplit.Length;   
  28.                     }   
  29.                     else if (endPos - beginPos > name.Length)   
  30.                     {   
  31.                         string objStr = val.Substring(beginPos, endPos - beginPos);   
  32.   
  33.                         string replaceValue = ReturnObjStringValue(objStr, obj, objType);   
  34.   
  35.                         ret.Append(val.Substring(lastAnaylsePos, beginPos - beginSplit.Length - lastAnaylsePos));   
  36.                         ret.Append(replaceValue);   
  37.                         lastAnaylsePos = endPos + endSplit.Length;   
  38.   
  39.                     }   
  40.                 }   
  41.                 beginPos = val.IndexOf(beginSplit, beginPos);   
  42.             }   
  43.             ret.Append(val.Substring( lastAnaylsePos));   
  44.             return ret.ToString();   
  45.         }   
  46.   
  47.         public  string ReturnObjStringValue(string inputStr, object obj, Type type)   
  48.         {   
  49.             char objDenotation = ‘.’;   
  50.             char[] methodDenotation = { ‘(’, ‘)’ };   
  51.             string ret = string.Empty;   
  52.   
  53.             int findPos = 0;   
  54.   
  55.             findPos = inputStr.IndexOf(objDenotation, findPos);   
  56.             if (findPos > 0)   
  57.             {   
  58.   
  59.                 int lastFindPos = -1;   
  60.                 int checkMenthodBound = inputStr.IndexOf( methodDenotation[0] , findPos+1 );   
  61.                 if (checkMenthodBound > -1)   
  62.                 {   
  63.                     lastFindPos = inputStr.IndexOf(objDenotation, findPos + 1, checkMenthodBound - findPos - 1);   
  64.                 }   
  65.                 else  
  66.                 {   
  67.                     lastFindPos = inputStr.IndexOf(objDenotation, findPos + 1);   
  68.                 }   
  69.                 if (lastFindPos == 0)   
  70.                 {   
  71.                     //do nothing , actully should be throw a error with syntax error   
  72.                 }   
  73.                 else if (lastFindPos > 0)   
  74.                 {   
  75.                     string objStr = inputStr.Substring(findPos + 1, lastFindPos - findPos - 1);   
  76.                     object tmpObj = ReturnObjFromPropertyOrMethod(objStr, obj, type);   
  77.                     if (null != tmpObj)   
  78.                     {   
  79.                         return ReturnObjStringValue(inputStr.Substring(findPos + 1), tmpObj, tmpObj.GetType());   
  80.                     }   
  81.                     else  
  82.                     {   
  83.                         //do nothing , actually should be throw a error with null with object   
  84.                     }   
  85.   
  86.                 }   
  87.                 else  
  88.                 {   
  89.                     object tmpObj = ReturnObjFromPropertyOrMethod(inputStr.Substring(findPos + 1), obj, type);   
  90.                     if (null != tmpObj)   
  91.                         ret = tmpObj.ToString();   
  92.                 }   
  93.             }   
  94.             else  
  95.             {   
  96.                 //do nothing , actually should be throw a error with syntax error   
  97.             }   
  98.   
  99.             return ret;   
  100.         }   
  101.   
  102.         public  object ReturnObjFromPropertyOrMethod(string inputStr, object obj, Type type)   
  103.         {   
  104.             char[] methodDenotation = { ‘(’, ‘)’ };   
  105.             char splitDenotation = ‘,’;   
  106.             char stringDenotaion = ‘”‘;   
  107.             char mathDenotation = ‘.’;   
  108.             object ret = null;   
  109.   
  110.             //analyse the object is a can reader property or public method   
  111.             int methodPos = inputStr.IndexOf(methodDenotation[0], 0);   
  112.             if (methodPos == 0)   
  113.             {   
  114.                 //do nothing , actully should be throw a error with syntax error   
  115.             }   
  116.             else if (methodPos > 0)   
  117.             {   
  118.                 //ensure there is a method   
  119.                 int methodPosEnd = inputStr.IndexOf(methodDenotation[1], methodPos + 1);   
  120.                 if (methodPosEnd > -1)   
  121.                 {   
  122.                     string methodName = inputStr.Substring(0, methodPos );   
  123.                     MethodInfo method = type.GetMethod(methodName);   
  124.                     if (null == method || !method.IsPublic)   
  125.                     {   
  126.                     }   
  127.                     else  
  128.                     {   
  129.                         if (methodPosEnd - methodPos == 1)   
  130.                         {   
  131.                             ret = method.Invoke(obj, null);   
  132.                         }   
  133.                         else  
  134.                         {   
  135.                             //nowing we are only support null,int,float,bool,string type input   
  136.                             string methodParamStr = inputStr.Substring(methodPos + 1, methodPosEnd - methodPos - 1);   
  137.                             string[] paramStrArr = methodParamStr.Split(splitDenotation);   
  138.                             object[] paramsObj = new object[paramStrArr.Length];   
  139.                             for (int i = 0; i < paramStrArr.Length; i++)   
  140.                             {   
  141.                                 if (paramStrArr[i][0] == stringDenotaion  )   
  142.                                 {   
  143.                                     if ( paramStrArr[i][paramStrArr[i].Length - 1] == stringDenotaion )   
  144.                                         paramsObj[i] = paramStrArr[i].Substring( 1 , paramStrArr[i].Length - 2 );   
  145.                                     else  
  146.                                         paramsObj[i] = null;   
  147.                                 }   
  148.                                 else if (paramStrArr[i] == “true” || paramStrArr[i] == “false”)   
  149.                                 {   
  150.                                     paramsObj[i] = bool.Parse(paramStrArr[i]);   
  151.                                 }   
  152.                                 else if (paramStrArr[i] == “null”)   
  153.                                 {   
  154.                                     paramsObj[i] = null;   
  155.                                 }   
  156.                                 else  
  157.                                 {   
  158.                                     try  
  159.                                     {   
  160.                                         if (paramStrArr[i].IndexOf(mathDenotation) > -1)   
  161.                                             paramsObj[i] = float.Parse(paramStrArr[i]);   
  162.                                         else  
  163.                                             paramsObj[i] = int.Parse(paramStrArr[i]);   
  164.                                     }   
  165.                                     catch  
  166.                                     {   
  167.                                         paramsObj[i] = null;   
  168.                                     }   
  169.                                 }   
  170.                             }   
  171.                             ret = method.Invoke(obj, paramsObj);   
  172.                         }   
  173.                     }   
  174.                 }   
  175.                 else  
  176.                 {   
  177.                     //do nothing , actully should be throw a error with syntax error   
  178.                 }   
  179.             }   
  180.             else  
  181.             {   
  182.                 //ensure there is a property   
  183.                 PropertyInfo property = type.GetProperty(inputStr);   
  184.                 if (null == property || !property.CanRead)   
  185.                 {   
  186.                     //do nothing , becasue there no matching Object Property to return   
  187.                 }   
  188.                 else  
  189.                 {   
  190.                     object tmpObj = property.GetValue(obj, null);   
  191.                     if (null != tmpObj)   
  192.                     {   
  193.                         ret = tmpObj;   
  194.                     }   
  195.                     else  
  196.                     {   
  197.                         //do nothing , actually should be throw a error with null with object   
  198.                     }   
  199.                 }   
  200.             }   
  201.             return ret;   
  202.         }   
  203.   
418 0 标签:C# 模版 
访客评论
    发表评论
    • 你的姓名:
    • 你的网站:
    •   EMAIL:   
    • 评论内容:
    • 私人