/// <summary> /// Attempts to emit the code to push a special parameter by looking it up in the interface parameters. /// If the parameter is defined, this emits a Ldarg operation. /// </summary> /// <param name="il">The ILGenerator to use.</param> /// <param name="parameterName">The name of the special parameter.</param> /// <param name="interfaceParameters">The parameters on the interface method.</param> /// <param name="executeParameters">The parameters on the execute method.</param> /// <returns>True if a parameter was emitted.</returns> private static bool EmitSpecialParameter(ILGenerator il, string parameterName, ParameterInfo[] interfaceParameters, ParameterInfo[] executeParameters) { // attempt to find the parameter on the interface method var interfaceParameter = interfaceParameters.FirstOrDefault(p => String.Compare(p.Name, parameterName, StringComparison.OrdinalIgnoreCase) == 0); if (interfaceParameter == null) return false; // attempt to find the parameter on the execute method var executeParameter = executeParameters.FirstOrDefault(p => String.Compare(p.Name, parameterName, StringComparison.OrdinalIgnoreCase) == 0); if (executeParameter == null) return false; // the types must match if (interfaceParameter.ParameterType != executeParameter.ParameterType) throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Special Parameter {0} must have type {1}", parameterName, executeParameter.ParameterType.FullName)); // the parameter list is 0-based, but 0 is the this pointer, so we add one il.Emit(OpCodes.Ldarg, (int)interfaceParameter.Position + 1); return true; }