Example #1
0
        private static void LoadParameters(CodeGenerator gen, ParameterInfo[] pis, bool isMethodStatic)
        {
            Check.Require(gen, "gen");

            if (pis != null)
            {
                for (int i = 0; i < pis.Length; ++i)
                {
                    if (isMethodStatic)
                    {
                        gen.Ldarg(0);
                    }
                    else
                    {
                        gen.Ldarg(1);
                    }
                    gen.Ldc(i);

                    Type srcType = pis[i].ParameterType;
                    string str = srcType.ToString();
                    if (str.EndsWith("&"))
                    {
                        srcType = CommonUtils.GetType(str.Substring(0, str.Length - 1));
                    }

                    if (str.EndsWith("&")) //ref or out param
                    {
                        if (srcType.IsValueType && (pis[i].Attributes & ParameterAttributes.Out) != ParameterAttributes.Out) //ref value param
                        {
                            gen.Ldelem(typeof(object));
                            gen.Unbox(srcType);
                        }
                        else
                        {
                            if (srcType.IsValueType && srcType != typeof(object)) //out value param
                            {
                                gen.LoadDefaultValue(srcType);
                                gen.Box(srcType);
                                gen.Stelem(typeof(object));

                                if (isMethodStatic)
                                {
                                    gen.Ldarg(0);
                                }
                                else
                                {
                                    gen.Ldarg(1);
                                }
                                gen.Ldc(i);
                                gen.Ldelem(typeof(object));
                                gen.Unbox(srcType);
                            }
                            else //ref or out class param
                            {
                                gen.Ldelema(typeof(object));
                            }
                        }
                    }
                    else
                    {
                        gen.Ldelem(typeof(object));

                        if (srcType.IsValueType)
                        {
                            gen.UnboxAny(srcType);
                        }
                        else if (srcType != typeof(object))
                        {
                            gen.Castclass(srcType);
                        }
                    }
                }
            }
        }