/// <summary> /// Stores the <paramref name="param">current parameter value</paramref> /// into the array of method <paramref name="arguments" />. /// </summary> /// <param name="IL">The <see cref="ILProcessor" /> that will be used to create the instructions.</param> /// <param name="arguments">The local variable that will store the method arguments.</param> /// <param name="index">The array index that indicates where the parameter value will be stored in the array of arguments.</param> /// <param name="param">The current argument value being stored.</param> private static void PushParameter(this ILProcessor IL, int index, VariableDefinition arguments, ParameterDefinition param) { var parameterType = param.ParameterType; IL.Emit(OpCodes.Ldloc, arguments); IL.Emit(OpCodes.Ldc_I4, index); // Zero out the [out] parameters if (param.IsOut || param.IsByRef()) { IL.Emit(OpCodes.Ldnull); IL.Emit(OpCodes.Stelem_Ref); return; } IL.Emit(OpCodes.Ldarg, param); if (parameterType.IsValueType || parameterType is GenericParameter) { IL.Emit(OpCodes.Box, param.ParameterType); } IL.Emit(OpCodes.Stelem_Ref); }
public void ShouldBeAbleToDetermineIfMethodIsByRef() { string location = typeof(SampleClassWithByRefMethod).Assembly.Location; AssemblyDefinition assembly = AssemblyFactory.GetAssembly(location); ModuleDefinition module = assembly.MainModule; TypeDefinition targetType = module.GetType("SampleClassWithByRefMethod"); MethodDefinition byRefMethod = targetType.GetMethod("ByRefMethod"); MethodDefinition regularMethod = targetType.GetMethod("NonByRefMethod"); Assert.IsNotNull(assembly); Assert.IsNotNull(targetType); Assert.IsNotNull(byRefMethod); Assert.IsNotNull(regularMethod); // Test the byref parameter ParameterDefinition parameter = byRefMethod.Parameters[0]; Assert.IsTrue(parameter.IsByRef()); // Test the non-byref parameter parameter = regularMethod.Parameters[0]; Assert.IsFalse(parameter.IsByRef()); }