Exemple #1
0
        protected override object VisitIEnumerable(IEnumerableCallSite enumerableCallSite, ILEmitResolverBuilderContext argument)
        {
            if (enumerableCallSite.ServiceCallSites.Length == 0)
            {
                argument.Generator.Emit(OpCodes.Call, ServiceLookupHelpers.GetArrayEmptyMethodInfo(enumerableCallSite.ItemType));
            }
            else
            {
                // var array = new ItemType[];
                // array[0] = [Create argument0];
                // array[1] = [Create argument1];
                // ...
                argument.Generator.Emit(OpCodes.Ldc_I4, enumerableCallSite.ServiceCallSites.Length);
                argument.Generator.Emit(OpCodes.Newarr, enumerableCallSite.ItemType);
                for (int i = 0; i < enumerableCallSite.ServiceCallSites.Length; i++)
                {
                    // duplicate array
                    argument.Generator.Emit(OpCodes.Dup);
                    // push index
                    argument.Generator.Emit(OpCodes.Ldc_I4, i);
                    // create parameter
                    ServiceCallSite parameterCallSite = enumerableCallSite.ServiceCallSites[i];
                    VisitCallSite(parameterCallSite, argument);
                    if (parameterCallSite.ServiceType.IsValueType)
                    {
                        argument.Generator.Emit(OpCodes.Unbox_Any, parameterCallSite.ServiceType);
                    }

                    // store
                    argument.Generator.Emit(OpCodes.Stelem, enumerableCallSite.ItemType);
                }
            }

            return(null);
        }
Exemple #2
0
        protected override Expression VisitIEnumerable(IEnumerableCallSite callSite, object context)
        {
            if (callSite.ServiceCallSites.Length == 0)
            {
                return(Expression.Constant(
                           ServiceLookupHelpers.GetArrayEmptyMethodInfo(callSite.ItemType)
                           .Invoke(obj: null, parameters: Array.Empty <object>())));
            }

            return(Expression.NewArrayInit(
                       callSite.ItemType,
                       callSite.ServiceCallSites.Select(cs =>
                                                        Convert(
                                                            VisitCallSite(cs, context),
                                                            callSite.ItemType))));
        }