{"id":308,"date":"2010-04-20T00:10:13","date_gmt":"2010-04-19T22:10:13","guid":{"rendered":"http:\/\/doena-journal.net\/en\/?p=308"},"modified":"2021-09-27T15:44:17","modified_gmt":"2021-09-27T13:44:17","slug":"c-codedom-inline-compiling","status":"publish","type":"post","link":"https:\/\/doena-journal.net\/en\/308\/c-codedom-inline-compiling","title":{"rendered":"C# CodeDom Inline Compiling"},"content":{"rendered":"<p>I was a bit bored just now and since the WYSIWYG editor of WordPress 2.9.2 doesn&#8217;t work as optimal as I had hoped, I decided to write my own program that translates forum posts with BB code into HTML.<\/p>\n<p>Of course I could have put all these replace functions into the code but then I would have to adapt the code every time I find a new tag.<\/p>\n<p>So I thought this might be a good time to toy with the Inline Compiler of C#. And if done right, it is relatively easy.<\/p>\n<p>Here&#8217;s my first Inline Compiling Code Snippet (it doesn&#8217;t contain any error handling and some such, but you get the idea how it works):<\/p>\n<p><!--more--><\/p>\n<table border=\"1\">\n<tr>\n<td>\n<pre>\r\nusing System;\r\nusing System.CodeDom.Compiler;\r\nusing Microsoft.CSharp;\r\nusing System.Xml.Serialization;\r\nusing System.IO;\r\nusing System.Text;\r\n\r\nnamespace DoenaSoft.InlineCompiling\r\n{\r\n  public static class Program\r\n  {\r\n    static void Main()\r\n    {   \r\n      ParamList pl;\r\n\r\n      \/\/to create a sample XML file:\r\n      pl = new ParamList();\r\n      pl.Params = new Param[1];\r\n      pl.Params[0] = new Param();\r\n      pl.Params[0].Name = \"Kursiver Text\";\r\n      pl.Params[0].Regex \r\n        = \"\\\\\\\\[i\\\\\\\\](?'Text'.+?)\\\\\\\\[\/i\\\\\\\\]\";\r\n      pl.Params[0].RegexOptions = \"RegexOptions.Compiled\"\r\n        + \"| RegexOptions.Singleline \"\r\n        + \"| RegexOptions.IgnoreCase\";\r\n      pl.Params[0].Code = \"\\\"<em>\\\"\"\r\n        + \" + match.Groups[\\\"Text\\\"].Value + \\\"<\/em>\\\"\";\r\n      using(FileStream fs = new FileStream(\"params.xml\"\r\n        , FileMode.Create, FileAccess.Write\r\n        , FileShare.None))\r\n      {\r\n        XmlSerializer xmls \r\n          = new XmlSerializer(typeof(ParamList));\r\n\r\n        xmls.Serialize(fs, pl);\r\n      }\r\n      \/\/end creating sample XML file\r\n\r\n      using(FileStream fs = new FileStream(\"params.xml\"\r\n        , FileMode.Open, FileAccess.Read\r\n        , FileShare.Read))\r\n      {\r\n        XmlSerializer xmls \r\n          = new XmlSerializer(typeof(ParamList));\r\n\r\n        pl = (ParamList)(xmls.Deserialize(fs));\r\n      }\r\n\r\n      String text = \"[i]some text[\/i]\";\r\n\r\n      foreach(Param param in pl.Params)\r\n      {\r\n        CSharpCodeProvider scp;\r\n        CompilerParameters cp;\r\n        CompilerResults cr;\r\n        Object replacer;\r\n        StringBuilder sb;\r\n\r\n        sb =  new StringBuilder();\r\n        sb.AppendLine(\"using System;\");\r\n        sb.AppendLine(\"using System.Text.\"\r\n          + \"RegularExpressions;\");\r\n        sb.AppendLine();\r\n        sb.AppendLine(\"public class RegexReplacer\");\r\n        sb.AppendLine(\"{\");\r\n        sb.AppendLine(\"  private static readonly Regex \"\r\n          + \"s_Regex = new Regex(\\\"\" + param.Regex \r\n          + \"\\\", \" + param.RegexOptions + \");\");\r\n        sb.AppendLine();\r\n        sb.AppendLine(\"  private String Replace(Match \"\r\n          + \"match)\");\r\n        sb.AppendLine(\"  {\");\r\n        sb.AppendLine(\"    return (\" + param.Code \r\n          + \");\");\r\n        sb.AppendLine(\"  }\");\r\n        sb.AppendLine();\r\n        sb.AppendLine(\"  public String Replace(String \"\r\n          + \"text)\");\r\n        sb.AppendLine(\"  {\");\r\n        sb.AppendLine(\"    return (s_Regex.Replace(text\"\r\n          + \", new MatchEvaluator(this.Replace)));\");\r\n        sb.AppendLine(\"  }\");\r\n        sb.AppendLine(\"}\");\r\n\r\n        scp = new CSharpCodeProvider();\r\n        cp = new CompilerParameters();\r\n        cp.ReferencedAssemblies.Add(\"System.dll\");\r\n        cr = scp.CompileAssemblyFromSource(cp\r\n          , sb.ToString());\r\n        replacer = cr.CompiledAssembly\r\n          .CreateInstance(\"RegexReplacer\");\r\n        text = replacer.GetType().GetMethod(\"Replace\")\r\n          .Invoke(replacer, new Object[] { text })\r\n          .ToString();\r\n      }\r\n    }\r\n  }\r\n\r\n  [Serializable()]\r\n  public class ParamList\r\n  {\r\n    [XmlArrayItem(\"Param\")]\r\n    public Param[] Params;\r\n  }\r\n\r\n  [Serializable()]\r\n  public class Param\r\n  {\r\n    public String Name;\r\n    public String Regex;\r\n    public String RegexOptions;\r\n    public String Code;\r\n  }\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>I was a bit bored just now and since the WYSIWYG editor of WordPress 2.9.2 doesn&#8217;t work as optimal as I had hoped, I decided&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,870],"tags":[1454,1453,1774,1455,1456,1773],"class_list":["post-308","post","type-post","status-publish","format-standard","hentry","category-misc","category-software","tag-net","tag-c","tag-csharpcodeprovider","tag-inline-compiling","tag-serialization","tag-xmlserializer"],"_links":{"self":[{"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/posts\/308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/comments?post=308"}],"version-history":[{"count":10,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"predecessor-version":[{"id":1194,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/posts\/308\/revisions\/1194"}],"wp:attachment":[{"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}