/// <summary>
        /// Emits the service method call.
        /// </summary>
        /// <param name="methodIL">The il emitter.</param>
        /// <param name="serviceMethod">The service method.</param>
        private void EmitServiceMethodCall(
            IEmitter methodIL,
            MethodInfo serviceMethod)
        {
            ILocal proxyArguments = null;

            if (this.methodParmAttrs.Any() == true)
            {
                // Load the proxy method arguments into an array.
                methodIL
                .DeclareLocal <object[]>("proxyArguments", out proxyArguments)

                .Array(
                    typeof(object),
                    proxyArguments,
                    this.methodParmTypes.Length,
                    (i) =>
                {
                    methodIL
                    .LdArg(i + 1)
                    .Conv(this.methodParmTypes[i], typeof(object), false);
                });

                methodIL.EmitWriteLine("------------------- PROXY ARGS --------------------");
                methodIL.EmitWriteLine(proxyArguments);

                // Load the service object.
                methodIL
                .LdArg0()
                .LdFld(this.context.BaseObjectField);

                // Iterate through service parameters
                for (int i = 0; i < this.methodParms.Length; i++)
                {
                    var fromModelAttr = this.methodParms[i].GetCustomAttribute <FromParameterAttribute>();
                    if (fromModelAttr != null)
                    {
                        if (string.IsNullOrEmpty(fromModelAttr.PropertyName) == false)
                        {
                            methodIL
                            .EmitLoadArrayElement(proxyArguments, this.methodParmIndex[fromModelAttr.ParameterName])
                            .LdStr(fromModelAttr.PropertyName)
                            .Call(BinderGetValueMethod.MakeGenericMethod(this.methodParms[i].ParameterType));
                        }
                        else
                        {
                            int index = this.methodParmIndex[fromModelAttr.ParameterName];
                            methodIL
                            .EmitLoadArrayElement(proxyArguments, index)
                            .EmitConverter(this.methodParmTypes[index], this.methodParms[i].ParameterType);
                        }

                        continue;
                    }

                    var fromHeaderAttr = this.methodParms[i].GetCustomAttribute <FromHeaderAttribute>();
                    if (fromHeaderAttr != null)
                    {
                        methodIL
                        .LdArg0()
                        .CallVirt(RequestPropertyInfo.GetGetMethod())
                        .CallVirt(HeadersPropertyInfo.GetGetMethod())
                        .LdStr(fromHeaderAttr.Name)
                        .CallVirt(ItemPropertyInfo.GetGetMethod());

                        continue;
                    }

                    methodIL.LdArg(i + 1);
                }

                // Call the service method
                methodIL
                .CallVirt(serviceMethod);
            }
            else
            {
                // Load the base object and method parameters then call the service method.
                methodIL
                .LdArg0()
                .LdFld(this.context.BaseObjectField)
                .EmitLoadParameters(this.methodParms)
                .CallVirt(serviceMethod);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Emits IL to call the set method of a property.
 /// </summary>
 /// <param name="emitter">The <see cref="IEmitter"/> to use.</param>
 /// <param name="propertyName">The property name.</param>
 /// <param name="propertyType">The property type.</param>
 /// <returns></returns>
 public static IEmitter SetProperty(this IEmitter emitter, string propertyName, Type propertyType)
 {
     var setMethod = propertyType.GetProperty(propertyName).GetSetMethod();
     return emitter
         .CallVirt(setMethod);
 }
        /// <summary>
        /// Emits IL to call the 'ToString' method on the object on the top of the evaluation stack.
        /// </summary>
        /// <param name="emitter">THe <see cref="IEmitter"/> to use.</param>
        public static IEmitter EmitToString(this IEmitter emitter)
        {
            MethodInfo toStringMethod = typeof(object).GetMethod("ToString", Type.EmptyTypes);

            return(emitter.CallVirt(toStringMethod));
        }
Esempio n. 4
0
 /// <summary>
 /// Emits IL to load the contents of a property onto the evaluation stack.
 /// </summary>
 /// <param name="emitter">The <see cref="IEmitter"/> to use.</param>
 /// <param name="propertyName">The property name.</param>
 /// <param name="propertyType">The property type.</param>
 /// <returns>The <see cref="IEmitter"/>.</returns>
 public static IEmitter GetProperty(this IEmitter emitter, string propertyName, Type propertyType)
 {
     MethodInfo getMethod = propertyType.GetProperty(propertyName).GetGetMethod();
     return emitter
         .CallVirt(getMethod);
 }