Scratch: A Head Start For Future Technologists

May 16th, 2007 Comments
Scratch

After the popularity of Logo (Turtle Graphics) in the 80′s, the Massachusetts Institute of Technology (MIT) has once again come up with a new programming language for kids called Scratch. Scratch teaches kids skills necessary to become future technology professionals, such as creative thinking, clear communication, systematic analysis, iterative design, and continuous learning, while they create fun and interesting projects such as interactive stories, graphic animations, games, music, and art. Projects created with Scratch can be shared on the web with others.

In addition to home, Scratch can be used by schools, museums, community centers, or other group settings. While the tool is intended for 8 to 16-year olds, with some adult help it can definitely be used by younger children.

Scratch is available for both Windows and Mac platforms, and is available as a free download.

JScript Eval Method In C#

February 6th, 2007 Comments
JScript

JavaScript and JScript languages provide a convenient Eval method that allows for dynamic evaluation of expressions. This functionality can be duplicated in C# using Microsoft’s JScript CodeDomProvider and Reflection.

Following class automatically compiles an in-memory assembly using JScript CodeDomProvider. The Eval method uses this assembly to dynamically evaluate specified expressions:

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;

///
/// Implements JScript/JavaScript Eval like
/// functionality in C# to allow dynamic evaluation
/// of expressions.
///
public static class ExpressionEvaluator
{
    #region Private Members

    private static Type _evaluatorType;
    private static object _evaluatorInstance;
    private static readonly string _jscriptEvalClass =
    @"import System;
class JScriptEvaluator
{
public static function Eval(expression : String) : String
{
return eval(expression);
}
}";

    #endregion

    #region Private Methods

    ///
    /// Creates a dynamic in-memory assembly using JScript.NET
    /// to evaluate expressions.
    ///
    private static void Initialize()
    {

        CodeDomProvider compiler = new JScriptCodeProvider();

        CompilerParameters parameters = new CompilerParameters();
        parameters.GenerateInMemory = true;
        parameters.ReferencedAssemblies.Add("system.dll");

        CompilerResults results = compiler.CompileAssemblyFromSource(parameters, _jscriptEvalClass);

        Assembly assembly = results.CompiledAssembly;
        _evaluatorType = assembly.GetType("JScriptEvaluator");
        _evaluatorInstance = Activator.CreateInstance(_evaluatorType);
    }

    #endregion

    #region Public Methods

    ///
    /// Evaluates specified expression using reflection
    /// on the assembly generated in Initialize() method.
    ///
    /// The expression.
    /// A string representing evaluated expression.
    public static string Eval(string expression)
    {
        if (_evaluatorInstance == null)
            Initialize();

        object result = _evaluatorType.InvokeMember(
        "Eval",
       BindingFlags.InvokeMethod,
        null,
       _evaluatorInstance,
        new object[] { expression }
       );

        return (result == null) ? null : result.ToString();

    }

    #endregion
}

Using this class, we can execute statements such as:

        Console.WriteLine(ExpressionEvaluator.Eval("2 + 9"));
        Console.WriteLine(ExpressionEvaluator.Eval("System.DateTime"));
        Console.WriteLine(ExpressionEvaluator.Eval("Environment.OSVersion.VersionString"));