#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 class ArrayGetEmitter : BaseEmitter
    {
        public ArrayGetEmitter(Type targetType)
            : base(new CallInfo(targetType, Flags.InstanceAnyVisibility, MemberTypes.Method, Constants.ArrayGetterName, new[] { typeof(int) }, null))
        {
        }

        internal protected override DynamicMethod CreateDynamicMethod()
        {
            return CreateDynamicMethod(Constants.ArrayGetterName, CallInfo.TargetType,
                Constants.ObjectType, new[] { Constants.ObjectType, Constants.IntType });
        }

        internal protected override Delegate CreateDelegate()
        {
            Type elementType = CallInfo.TargetType.GetElementType();
            Generator.ldarg_0 // load array
                .castclass(CallInfo.TargetType) // (T[])array
                .ldarg_1 // load index
                .ldelem(elementType) // load array[index]
                .boxIfValueType(elementType) // [box] return
                .ret();
            return Method.CreateDelegate(typeof(ArrayElementGetter));
        }
    }
}
