Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="procName"></param>
        /// <param name="args"></param>
        public static void Proc(this object obj, string procName, params object[] args)
        {
            Guard.NotNull(obj, "obj");
            Guard.NotNull(procName, "procName");

            ReflectionService.Func(obj, procName, args);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ilGenerator"></param>
        /// <param name="enumerable"></param>
        public static void LoadEnumerable(this ILGenerator ilGenerator, IEnumerable enumerable)
        {
            // We load enumerable as an array - this is the most compact and efficient way of representing it
            Type elementType = null;
            Type closedType  = null;

            if (ReflectionService.TryGetGenericInterfaceType(enumerable.GetType(), Types.IEnumerableofT, out closedType))
            {
                elementType = closedType.GetGenericArguments()[0];
            }
            else
            {
                elementType = Types.Object;
            }

            //
            // elem[] array = new elem[<enumerable.Count()>]
            //
            Type         generatedArrayType  = elementType.MakeArrayType();
            LocalBuilder generatedArrayLocal = ilGenerator.DeclareLocal(generatedArrayType);

            ilGenerator.LoadInt(enumerable.Cast <object>().Count());
            ilGenerator.Emit(OpCodes.Newarr, elementType);
            ilGenerator.Emit(OpCodes.Stloc, generatedArrayLocal);

            int index = 0;

            foreach (var value in enumerable)
            {
                //
                //array[<index>] = value;
                //
                ilGenerator.Emit(OpCodes.Ldloc, generatedArrayLocal);
                ilGenerator.LoadInt(index);
                ilGenerator.LoadValue(value);
                //if (EmitHelper.IsBoxingRequiredForValue(value) && !elementType.IsValueType)
                //{
                //    ilGenerator.Emit(OpCodes.Box, value.GetType());
                //}
                if (value != null)
                {
                    ilGenerator.UnboxOrCast(value.GetType());
                }

                ilGenerator.Emit(OpCodes.Stelem, elementType);
                index++;
            }

            ilGenerator.Emit(OpCodes.Ldloc, generatedArrayLocal);
        }