Login to TechLifeForum X
Board Index Go to Reboot.Pro
  • Account Login
  • Register
TechLifeForum
  • Home
  • Members
  • Awards
  • Rules
  • Help
  • Donate
  • Live IRC
TechLifeForum / Programming and Development / .Net Framework Programming v
« Previous 1 ... 17 18 19 20 21 ... 46 Next »
/  SourceRunner - CodeDOM Invocation Example[Advanced Search]
Reply to thread
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

[Source] SourceRunner - CodeDOM Invocation Example

06-25-2012, 03:00 AM (This post was last modified: 06-25-2012 03:07 AM by AceInfinity.)
Post: #1
AceInfinity Offline
∞ IҊϜIɴITϵ ☭
*****
Microsoft MVP
Posts: 8,696
Joined: Jun 2011
Reputation: 61
SourceRunner - CodeDOM Invocation Example


Features:
    • Compile from textbox or file
    • Change .NET Framework version
    • Change reference assemblies

Just a quick example i've put together as we haven't had much around here on CodeDOM at all... With this setup, and I haven't tested yet even though I should have, unsafe code context should work as well. :)

Code: (SELECT ALL CODE)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace SourceRunner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 2;
            textBox1.Text = "using System;\r\n" +
                            "using System.Collections.Generic;\r\n" +
                            "using System.ComponentModel;\r\n" +
                            "using System.Data;\r\n" +
                            "using System.Drawing;\r\n" +
                            "using System.Linq;\r\n" +
                            "using System.Text;\r\n" +
                            "using System.Windows.Forms;\r\n" +
                            "\r\n" +
                            "namespace CDNamespace\r\n" +
                            "{\r\n" +
                            "    public class CodeClass\r\n" +
                            "    {\r\n" +
                            "        public void Main()\r\n" +
                            "        {\r\n" +
                            "            //Entry Point\r\n" +
                            "        }\r\n" +
                            "    }\r\n" +
                            "}";
            textBox1.SelectionStart = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RunCode();
        }

        private void RunCode()
        {
            string ver = "v3.5";

            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    ver = "v2.0";
                    break;
                case 1:
                    ver = "v3.0";
                    break;
                case 2:
                    ver = "v3.5";
                    break;
                case 3:
                    ver = "v4.0";
                    break;
            }

            string[] AssemInfo = File.ReadAllLines(Application.StartupPath + "\\ReferenceAssemblies.txt").Where(l => !l.StartsWith(@"//") && l.Contains(".dll")).ToArray();
            Dictionary providerOptions = new Dictionary { { "CompilerVersion", ver } };
            CSharpCodeProvider CSCodeProvider = new CSharpCodeProvider(providerOptions);

            CompilerParameters CParams = new CompilerParameters();
            CParams.GenerateInMemory = true;
            CParams.CompilerOptions = "/optimize /unsafe";
            CParams.ReferencedAssemblies.AddRange(AssemInfo);
            //CParams.TempFiles = new TempFileCollection(Application.StartupPath + "\\TempFiles", false);

            string CodePart = radioButton1.Checked ? File.ReadAllText(Application.StartupPath + "\\CompileCode.txt") : textBox1.Text;

            CompilerResults objCompileResults = CSCodeProvider.CompileAssemblyFromSource(CParams, CodePart);
            if (objCompileResults.Errors.HasErrors)
            {
                string[] Errors = objCompileResults.Errors.Cast().Select(e => string.Format("Error [Line: {0}]: {1}", e.Line.ToString(), e.ErrorText)).ToArray();
                MessageBox.Show(string.Join("\n", Errors), string.Format("[{0}] Compiler Errors", Errors.Count()), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            object AssemblyInst = objCompileResults.CompiledAssembly.CreateInstance("CDNamespace.CodeClass");

            try
            {
                if (AssemblyInst != null)
                    AssemblyInst.GetType().InvokeMember("Main", System.Reflection.BindingFlags.InvokeMethod, null, AssemblyInst, null);
                else
                    MessageBox.Show("Cannot create instance of assembly...", "Null Object", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An error had occurred:\n\n{0}", ex.Message), "InvokeMember Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}

For the text files which have to be in the same working directory as the app at runtime:

CompileCode.txt:
Code: (SELECT ALL CODE)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDNamespace
{
    public class CodeClass
    {
        public void Main()
        {
            //Entry point
        }
    }
}

ReferenceAssemblies.txt: *I just added a whole bunch of crap here after I added in the main ones.
Code: (SELECT ALL CODE)
//This is a list of Referenced Assemblies

mscorlib.dll
System.dll
System.Core.dll
System.Data.dll
System.Drawing.dll
System.Windows.Forms.dll
System.Configuration.dll
System.Xml.dll
System.Security.dll
System.Web.RegularExpressions.dll
System.Runtime.Serialization.Formatters.Soap.dll

Download: To the one I compiled if you can't compile your own here.
*See Attachment*


Attached File(s)
.zip  SourceRunner.zip (Size: 59.27 KB / Downloads: 13)



Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer


Development Site: aceinfinity.net
WWW Find
Reply
06-25-2012, 03:11 AM
Post: #2
AceInfinity Offline
∞ IҊϜIɴITϵ ☭
*****
Microsoft MVP
Posts: 8,696
Joined: Jun 2011
Reputation: 61
RE: SourceRunner - CodeDOM Invocation Example
EDIT: Yep, unsafe code works too



cheers



Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer


Development Site: aceinfinity.net
WWW Find
Reply
06-25-2012, 07:54 AM
Post: #3
kerplunk Offline
Legen.. *wait for it* ..dary!
***
Posts: 259
Joined: Dec 2011
Reputation: 1
RE: SourceRunner - CodeDOM Invocation Example
Wow nice! this tool could be handy for testing snippets :)
Find
Reply
06-25-2012, 09:11 AM
Post: #4
KoBE Offline
¯\_(ツ)_/¯
*******
Administrators
Posts: 4,197
Joined: Jun 2011
Reputation: 47
RE: SourceRunner - CodeDOM Invocation Example
As you said, we don't get much CodeDOM around here. Nice work Ace!

-> TechLifeForum on Facebook<- 
-> TechLifeForum on YouTube<-
-> TechLifeForum on Twitter<-

Tech.Reboot.Pro
WWW Find
Reply
06-25-2012, 09:54 PM
Post: #5
AceInfinity Offline
∞ IҊϜIɴITϵ ☭
*****
Microsoft MVP
Posts: 8,696
Joined: Jun 2011
Reputation: 61
RE: SourceRunner - CodeDOM Invocation Example
(06-25-2012 07:54 AM)#KerplunK Wrote:  Wow nice! this tool could be handy for testing snippets :)

Exactly, if you don't feel like opening VS? It'll do for small functions, depending on what they do.

(06-25-2012 09:11 AM)KoBE Wrote:  As you said, we don't get much CodeDOM around here. Nice work Ace!

I haven't seen stuff like this, this was a test I figured out all on my own. In this source i've set the /optimize flag and /unsafe flag, if you don't want unsafe code to be allowed, then remove that, or /optimize.

You can change .NET framework versions. By default it assumes 2.0 I believe, which is useless. I wasn't able to use LINQ until I added it to compile using v3.5 or higher.
cheers



Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer


Development Site: aceinfinity.net
WWW Find
Reply
Reply to thread


  • View a Printable Version
  • Send this Thread to a Friend
  • Subscribe to this thread
Forum Jump:


Users browsing this thread
1 Guest(s)
Youtube Facebook Twitter Digg
Return to Top
All content © copyright TechLifeForum
Powered By MyBB, © 2002-2013 MyBB Group
Designed by ThemeFreak
Mobile Version