Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Implementing Programming Languages Using C# 4.0

, 12 Jul 2012
An introduction to creating programming language tools using C# 4.0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;

namespace Diggins.Jigsaw
{
    public static class Utilities
    {
        public static Type GetType(string s)
        {
            s = s.Trim();
            switch (s)
            {
                case "int": return typeof(int);
                case "uint": return typeof(uint);
                case "char": return typeof(char);
                case "float": return typeof(float);
                case "double": return typeof(double);
                case "bool": return typeof(bool);
                case "byte": return typeof(byte);
                case "sbyte": return typeof(sbyte);
                case "object": return typeof(object);
                case "decimal": return typeof(decimal);
                case "string": return typeof(string);
                default:
                    var r = Type.GetType(s);
                    if (r != null) return r;
                    r = Type.GetType("System." + s);
                    if (r != null) return r;
                    throw new Exception("Could not find type " + s);
            }
        }

        public static void TimeExecution(Action a)
        {
            Console.WriteLine("Starting execution");
            var start = DateTime.Now;
            a();
            var elapsed = DateTime.Now - start;
            Console.WriteLine("Time of execution is {0} sec, {1} msec", elapsed.TotalSeconds, elapsed.Milliseconds);
        }

        public static void RunMain(Assembly asm, params string[] args)
        {
            MethodInfo main = null;
            foreach (var t in asm.GetTypes())
                foreach (var mi in t.GetMethods(BindingFlags.Public | BindingFlags.Static))
                    if (mi.Name == "Main")
                       main = mi;
            main.Invoke(null, new object[] { args });
        }

        public static string Unquote(string s)
        {
            return s.Substring(1, s.Length - 2);
        }

        public static void Test()
        {
            Func<object, object> f = x => x;
            Delegate d = f;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Christopher Diggins
Software Developer Autodesk
Canada Canada
This article was written by Christopher Diggins, a computer science nerd who currently works at Autodesk as an SDK specialist.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 12 Jul 2012
Article Copyright 2011 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid