Transcript Document
Compilerbau .NET Compiler / Codegeneratoren / Skriptsprachen Dipl. Inf. (FH) Paul Mizel Email: [email protected] Inhalt Skriptsprachen Motivation Codegenerierung mit CodeDom Codemodifizierung mit Reflections Codekompilierung Eigene C# Scriptsprache Demos Skriptsprachen Clientseitig JavaScript VBScript Serverseitig PHP Perl Python Anwendungen Lua – Wird in Spiel-Entwicklung eingesetzt REXX – Skriptsprache von IBM Motivation Code Generierung aus dem Code heraus?!?!? in verschiedene .NET Sprachen. using System.CodeDom; Wie kann ich Bibliothekencode Dekompilieren und Modifizieren? using System.Reflection; using Microsoft.CSharp; using System.CodeDom; Wie kann ich mit .NET Kompilieren? using Microsoft.CSharp; Wie erzeuge ich eigene Scriptsprache? using Microsoft.CSharp; Codegenerierung namespace XYZ { public class YZX { public static void Main() { string data=„xyz“; System.Console.WriteLine(data); } } } CodeNamespace CodeTypeDeclaration Type:Add CodeEntryPointMethod Members:Add CodeVariableDeclarationStatement Statements:Add CodeMethodInvokeExpression Statements:Add Codegenerierung System.CodeDom .NET Typengleichheit string msg; msg = "hello world"; System.Console.WriteLine(message); CodeVariableDeclarationStatement strDecl = new CodeVariableDeclarationStatement( new CodeTypeReference(typeof(string)), “msg"); mainMethod.Statements.Add(strDecl); CodeAssignStatement ptxAssign = new CodeAssignStatement(new CodeVariableReferenceExpression(„msg"), new CodeSnippetExpression("\"hello world\"")); mainMethod.Statements.Add(ptxAssign); CodeMethodInvokeExpression invokeConsoleWriteLine = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(Console)), "WriteLine", new CodeExpression[] { new CodeArgumentReferenceExpression(„msg"), }); mainMethod.Statements.Add(invokeConsoleWriteLine); Disassemblierung Modifizierung System.Reflection (read) System.Reflection.FieldInfo[] fia = (object)o.GetType().GetFields(); System.CodeDom (write) Codegenerierung Object.GetType() Type.GetFields() FieldInfo[] Codekompilieren im Speicher public void Start(string code) { CSharpCodeProvider c = new CSharpCodeProvider(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Data.dll"); cp.ReferencedAssemblies.Add("System.Windows.Forms.dll"); cp.GenerateExecutable = true; cp.GenerateInMemory = true; CompilerResults r = c.CompileAssemblyFromSource(cp, code); System.String[] scriptArgs = new System.String[1]; r.CompiledAssembly.EntryPoint.Invoke(null, scriptArgs); } C# als Skriptsprache Interface public interface IScript { int main(out object output, params object[] input); } Script using xyz; namespace Compilerbau { public class FILENAME : IScript { MAIN_CODE } } Beispiel für MAIN_CODE window.script public int main(out object output, params object[] input) { output =(object)"Windows Beispiel"; Win w = new Win(); w.Text="Compilerbau"; w.Show(); return 0; } public class Win : Form{} Was am Ende steht using xyz; namespace Compilerbau { public class FILENAME : IScript { //MAIN_CODE_BEGIN public int main(out object output, params object[] input) { output =(object)"Windows Beispiel"; Win w = new Win(); w.Text="Compilerbau"; w.Show(); return 0; } public class Win : Form{} //MAIN_CODE_END } } DEMO CodeCompiler CodeGenerierung CodeDekomilierung SkriptLive C# als Skriptsprache Inhaltsverzeichnis Microsoft MSDN msdn.microsoft.com Code Generation in multiple languages blogs.msdn.com/abhinaba/archive/2006/02/27/539702.aspx C#: Writing extendable applications using on-the-fly compilation blogs.msdn.com/abhinaba/archive/2006/02/09/528416.aspx