Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

.Net Expression Evaluator using DynamicMethod

Rate me:
Please Sign up or sign in to vote.
4.97/5 (23 votes)
13 Mar 20076 min read 120.2K   2.1K   102  
Evaluating Dynamic expressions by compiling C# code to IL, then creating a DynamicMethod from the IL.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;

namespace ExpressionEval.MethodState
{
    /// <summary>
    /// A serialized version of a method.  Holds everything to create a DynamicMethod.
    /// </summary>
    [Serializable]
    public class DynamicMethodState
    {
        /// <summary>
        /// The IL bytes from compilation
        /// </summary>
        public byte[] CodeBytes;

        /// <summary>
        /// Whether or not local variables are initialized
        /// </summary>
        public bool InitLocals;

        /// <summary>
        /// The maximum size of the stack required for this method
        /// </summary>
        public int MaxStackSize;

        /// <summary>
        /// Local variables defined for the method
        /// </summary>
        public IDictionary<int,LocalVariable> LocalVariables;

        /// <summary>
        /// Definition of tokens that need to be resolved
        /// </summary>
        public TokenOffset TokenOffset;

    }

    /// <summary>
    /// Defines a local variable for a method
    /// </summary>
    [Serializable]
    public class LocalVariable
    {
        /// <summary>
        /// Whether or not the variable is pinned in memory, used for working with unmanaged memory
        /// </summary>
        public bool IsPinned;

        /// <summary>
        /// Type of the variable
        /// </summary>
        public RuntimeTypeHandle LocalType;

        public LocalVariable(bool isPinned, RuntimeTypeHandle localType)
        {
            IsPinned = isPinned;
            LocalType = localType;
        }


    }

    /// <summary>
    /// A definition of tokens to resolve for given offsets in IL code bytes
    /// </summary>
    [Serializable]
    public class TokenOffset
    {
        /// <summary>
        /// Fields to be resolved
        /// </summary>
        public IDictionary<int, RuntimeFieldHandle> Fields;

        /// <summary>
        /// Method to be resolved
        /// </summary>
        public IDictionary<int, MethodBase> Methods;

        /// <summary>
        /// Types to be resolved
        /// </summary>
        public IDictionary<int, RuntimeTypeHandle> Types;

        /// <summary>
        /// Literal strings to resolve
        /// </summary>
        public IDictionary<int, string> LiteralStrings;
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
.Net Software Engineer in Kansas, USA trying to keep pace with technology.

Comments and Discussions