Monday, 9 January 2012

Compile C# into assembly, then load it and execute it

This is one way I have found that I can compile code at runtime into a DLL file, then load it and call it. The code I will be compiling is this:

class FruitVendor
{
    private string fruits = "";

    public FruitVendor()
    {
        fruits = "apples & bananas";
    }

    public string GetFruit()
    {
        return fruits;
    }
}

I have two texts boxes, one for input and one for output. And in order to compile & run it, I do this:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.CodeDom.Compiler;

namespace ProjectName
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
            string outputFilename = "fruits.dll";
            Button ButtonObject = (Button)sender;
            
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();            
            parameters.GenerateExecutable = false;
            parameters.OutputAssembly = outputFilename;
            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox1.Text);

            if (results.Errors.Count > 0)
            {
                textBox2.Text = "";
                textBox2.ForeColor = Color.Red;                
                foreach (CompilerError CompErr in results.Errors)
                {
                    textBox2.Text = textBox2.Text +
                        "Line number " + CompErr.Line +
                        ", Error Number: " + CompErr.ErrorNumber +
                        ", '" + CompErr.ErrorText + ";" +
                        Environment.NewLine + Environment.NewLine;
                }
            }
            else
            {
                textBox2.ForeColor = Color.Black;

                string path = System.Environment.CurrentDirectory + "\\" + outputFilename;
                Assembly assembly = Assembly.LoadFrom(path);
                Type type = assembly.GetType("FruitVendor");
                MethodInfo mi = type.GetMethod("GetFruit");
                object result = mi.Invoke(Activator.CreateInstance(type), null);                

                textBox2.Text = (string)result; ;
            }
        }

    }
}

It works a treat and is pretty quick too. Thanks to everyone over at StackOverflow, this thread helped a lot!

Tuesday, 22 November 2011

MySQL Global Vars wait_timeout & max_connections

Just some more code for me to bookmark. Problem was the MySQL connections were not being closed, the timeout was set at 8 hours and the connection limit was 400.

SHOW VARIABLES LIKE "%wait%"
SET @@GLOBAL.wait_timeout=300

SHOW VARIABLES LIKE "%max%"
SET @@GLOBAL.max_connections=1000

Monday, 21 November 2011

MySQL Relationships & Foreign Keys

Just another snippet of code that I will forget..

ALTER TABLE `accounts`
  ADD CONSTRAINT `FK_myKey` FOREIGN KEY (`customer_id`) 
  REFERENCES `customers` (`customer_id`) 
  ON DELETE CASCADE ON UPDATE CASCADE;

Thanks

Friday, 23 September 2011

HOW TO HANDLE TWITTER SPAMMERS

The problem with Twitter is that you never know whether someone is a spammer with malicious links or if they're a genuine person. While you can read their profile and work it out, this is generally too time consuming especially when my you get on average 3 spammers following you an hour! My Interstellar Workshop twitter account got 6 spamtarts following it within the first few hours!

I propose an Up/Down vote reputation system for Twitter profiles, this could also be used on other non-twitter platforms too. The users would be able to up/down vote a profile, such as up voting Evil Wil Wheaton and down voting spamtarts. Then you could flag the spam tarts, if they have 1) added lots of people recently and 2) have a low reputation, you could suspend them and require they do some verification steps, which the bots wont do!

View post at my website: HOW TO HANDLE TWITTER SPAMMERS

Saturday, 3 September 2011

Personal Website update

I have been working on my new personal website lately - www.adamkdean.co.uk - which I will integrate with this blog as well as other blogs of mine, to centralise all of the content I put out there.

Check it out!