1 / 13

Compilerbau

Compilerbau. .NET Compiler / Codegeneratoren / Skriptsprachen Paul Mizel pmizel@do-dotnet.de. Inhalt. Skriptsprachen Motivation Codegenerierung mit CodeDom Codemodifizierung mit Reflections Codekompilierung Eigene C# Scriptsprache Demos. Skriptsprachen. Clientseitig JavaScript

makya
Télécharger la présentation

Compilerbau

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compilerbau .NET Compiler / Codegeneratoren / Skriptsprachen Paul Mizel pmizel@do-dotnet.de

  2. Inhalt • Skriptsprachen • Motivation • Codegenerierung mit CodeDom • Codemodifizierung mit Reflections • Codekompilierung • Eigene C# Scriptsprache • Demos

  3. Skriptsprachen • Clientseitig • JavaScript • VBScript • Serverseitig • PHP • Perl • Python • Anwendungen • Lua – Wird in Spiel-Entwicklung eingesetzt • REXX – Skriptsprache von IBM

  4. Motivation • Code Generierung aus dem Code heraus?!?!? in verschiedene .NET Sprachen. usingSystem.CodeDom; • Wie kann ich BibliothekencodeDekompilieren und Modifizieren? usingSystem.Reflection; usingMicrosoft.CSharp; usingSystem.CodeDom; • Wie kann ich mit .NET Kompilieren? usingMicrosoft.CSharp; • Wie erzeuge ich eigene Scriptsprache? usingMicrosoft.CSharp;

  5. Codegenerierung namespace XYZ { publicclass YZX { publicstaticvoid Main() { stringdata=„xyz“; System.Console.WriteLine(data); } } }

  6. Codegenerierung • System.CodeDom • .NET Typengleichheit 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); string msg; msg = "hello world"; System.Console.WriteLine(message);

  7. Disassemblierung Modifizierung • System.Reflection(read) System.Reflection.FieldInfo[] fia = (object)o.GetType().GetFields(); • System.CodeDom(write) Codegenerierung

  8. 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); }

  9. 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 } }

  10. 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{}

  11. Was am Ende steht using xyz; namespace Compilerbau { public class FILENAME : IScript { //MAIN_CODE_BEGINpublic 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 } }

  12. CodeCompiler CodeGenerierung CodeDekomilierung SkriptLive C# als Skriptsprache

  13. 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

More Related