#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Reflection.Emit
{
    internal abstract class BaseEmitter_
    {
        protected CallInfo_ CallInfo;
        protected DelegateCache Cache;

        protected BaseEmitter_(CallInfo_ callInfo, DelegateCache cache)
        {
            this.CallInfo = callInfo;
            this.Cache = cache;
        }

        protected abstract Delegate CreateDelegate();
        protected abstract object Invoke(Delegate action);

        public Delegate GetDelegate()
        {
            return Cache.GetDelegate(CallInfo, CreateDelegate);
        }

        public void Execute()
        {
            Invoke(GetDelegate());
        }

        public T Execute<T>()
        {
            return (T)Invoke(GetDelegate());
        }

        protected static DynamicMethod CreateDynamicMethod(string name, Type targetType, Type returnType, Type[] paramTypes)
        {
            return new DynamicMethod(name, 
                MethodAttributes.Static | MethodAttributes.Public,
                CallingConventions.Standard, 
                returnType, 
                paramTypes, 
                targetType, 
                true);
        }

        protected void BoxIfValueType(ILGenerator generator, Type type)
        {
            if (type.IsValueType)
            {
                generator.Emit(OpCodes.Box, type);
            }
        }

        protected void UnboxOrCast(ILGenerator generator, Type type)
        {
            generator.Emit(type.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, type);
        }

        protected BindingFlags ScopeFlag
        {
            get { return CallInfo.IsStatic ? BindingFlags.Static : BindingFlags.Instance; }
        }
    }
}
