本文整理汇总了C#中Mono.Cecil.Cil.ILProcessor.Emit方法的典型用法代码示例。如果您正苦于以下问题:C# ILProcessor.Emit方法的具体用法?C# ILProcessor.Emit怎么用?C# ILProcessor.Emit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.Cil.ILProcessor
的用法示例。
在下文中一共展示了ILProcessor.Emit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetServiceHash
/// <summary>
/// Emits a call that obtains the hash code for the current service instance.
/// </summary>
/// <param name="il">The <see cref="ILProcessor"/> that points to the method body.</param>
/// <param name="module">The target module.</param>
/// <param name="serviceInstance">The local variable that contains the service instance.</param>
private void GetServiceHash(ILProcessor il, ModuleDefinition module, VariableDefinition serviceInstance)
{
il.Emit(OpCodes.Ldloc, serviceInstance);
var getHashCodeMethod = module.ImportMethod<object>("GetHashCode");
il.Emit(OpCodes.Callvirt, getHashCodeMethod);
}
示例2: TestSequence1
private static void TestSequence1(ILProcessor il)
{
TypeReference type = new TypeReference("", "Test", null, null);
FieldDefinition blank = new FieldDefinition("Test", FieldAttributes.Public, type);
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ldsfld, blank);
il.Emit(OpCodes.Stsfld, blank);
il.Emit(OpCodes.Ret);
}
示例3: ImplementProceed
protected override void ImplementProceed(MethodDefinition methodInfo, MethodBody methodBody, ILProcessor il, FieldReference methodInfoField, MethodReference proceed, Action<ILProcessor> emitProceedTarget, MethodReference proceedTargetMethod, OpCode proceedOpCode)
{
// If T is an interface, then we want to check if target is null; if so, we want to just return the default value
var targetNotNull = il.Create(OpCodes.Nop);
EmitProxyFromProceed(il);
il.Emit(OpCodes.Ldfld, ClassWeaver.Target); // Load "target" from "this"
il.Emit(OpCodes.Brtrue, targetNotNull); // If target is not null, jump below
CecilExtensions.CreateDefaultMethodImplementation(methodBody.Method, il);
il.Append(targetNotNull); // Mark where the previous branch instruction should jump to
base.ImplementProceed(methodInfo, methodBody, il, methodInfoField, proceed, emitProceedTarget, proceedTargetMethod, proceedOpCode);
}
示例4: CompileTarget
public static void CompileTarget(ILProcessor processor, IAstExpression target, IAstMethodReference function, CilCompilationContext context)
{
context.Compile(target);
if (function.Location == MethodLocation.Extension)
return;
var targetType = context.ConvertReference(target.ExpressionType);
if (!targetType.IsValueType)
return;
var variable = context.DefineVariable("x", targetType);
processor.Emit(OpCodes.Stloc, variable);
processor.Emit(OpCodes.Ldloca_S, variable);
}
示例5: AddFieldConstructorArgument
private void AddFieldConstructorArgument(MethodDefinition constructor, FieldDefinition field, int argumentId, ILProcessor processor)
{
constructor.Parameters.Add(new ParameterDefinition(field.Name, ParameterAttributes.None, field.FieldType));
processor.Emit(OpCodes.Ldarg_0);
if (argumentId == 1) {
processor.Emit(OpCodes.Ldarg_1);
} else if (argumentId == 2) {
processor.Emit(OpCodes.Ldarg_2);
} else if (argumentId == 3) {
processor.Emit(OpCodes.Ldarg_3);
} else {
processor.Emit(OpCodes.Ldarg, argumentId);
}
processor.Emit(OpCodes.Stfld, field);
}
示例6: EmitAddress
public override void EmitAddress(ILProcessor ilProcessor)
{
if (Previous.Type.Resolve().IsValueType)
Previous.EmitAddress(ilProcessor);
else
Previous.Emit(ilProcessor);
ilProcessor.Emit(OpCodes.Ldflda, Field);
}
示例7: EmitGetContainerInstance
/// <summary>
/// Emits the instructions that will obtain the <see cref="IMicroContainer"/> instance.
/// </summary>
/// <param name="module">The target module.</param>
/// <param name="microContainerType">The type reference that points to the <see cref="IMicroContainer"/> type.</param>
/// <param name="worker">The <see cref="CilWorker"/> that points to the <see cref="IMicroContainer.GetInstance"/> method body.</param>
/// <param name="skipCreate">The skip label that will be used if the service cannot be instantiated.</param>
protected override void EmitGetContainerInstance(ModuleDefinition module, TypeReference microContainerType, ILProcessor il, Instruction skipCreate)
{
var getNextContainer = module.ImportMethod<IMicroContainer>("get_NextContainer");
EmitGetNextContainerCall(il, microContainerType, getNextContainer);
il.Emit(OpCodes.Brfalse, skipCreate);
// var result = NextContainer.GeService(serviceType, serviceName);
EmitGetNextContainerCall(il, microContainerType, getNextContainer);
}
示例8: HandleInterceptReturnValue
private static void HandleInterceptReturnValue(MethodDefinition method, ILProcessor processor)
{
if (method.ReturnType.IsValueType)
{
// unbox
processor.Emit(OpCodes.Unbox_Any, method.ReturnType);
}
else if (method.ReturnType == WeavingInformation.ModuleDefinition.TypeSystem.Void)
{
// remove return value of intercept method from stack
processor.Emit(OpCodes.Pop);
}
else
{
// cast to reference type
processor.Emit(OpCodes.Castclass, method.ReturnType);
}
}
示例9: ImplementProceed
protected override void ImplementProceed(MethodDefinition methodInfo, MethodBody methodBody, ILProcessor il, FieldReference methodInfoField, MethodReference proceed, Action<ILProcessor> emitProceedTarget, MethodReference proceedTargetMethod, OpCode proceedOpCode)
{
if (methodInfo.IsAbstract)
{
CecilExtensions.CreateDefaultMethodImplementation(methodInfo, il);
}
else
{
var targetNotNull = il.Create(OpCodes.Nop);
EmitProxyFromProceed(il);
il.Emit(OpCodes.Ldfld, target); // Load "target" from "this"
il.Emit(OpCodes.Brtrue, targetNotNull); // If target is not null, jump below
base.ImplementProceed(methodInfo, methodBody, il, methodInfoField, proceed, _ => EmitProxyFromProceed(il), callBaseMethod, OpCodes.Call);
il.Append(targetNotNull); // Mark where the previous branch instruction should jump to
base.ImplementProceed(methodInfo, methodBody, il, methodInfoField, proceed, emitProceedTarget, proceedTargetMethod, proceedOpCode);
}
}
示例10: EmitEntryPoint
static void EmitEntryPoint(FieldDefinition entry_points, ILProcessor il, int slot)
{
il.Emit(OpCodes.Ldsfld, entry_points);
il.Emit(OpCodes.Ldc_I4, slot);
il.Emit(OpCodes.Ldelem_I);
}
示例11: EmitCalli
static void EmitCalli(ILProcessor il, MethodReference reference)
{
var signature = new CallSite(reference.ReturnType)
{
CallingConvention = MethodCallingConvention.StdCall,
};
foreach (var p in reference.Parameters)
{
signature.Parameters.Add(p);
}
// Since the last parameter is always the entry point address,
// we do not need any special preparation before emiting calli.
il.Emit(OpCodes.Calli, signature);
}
示例12: EmitConvenienceWrapper
static void EmitConvenienceWrapper(MethodDefinition wrapper,
MethodDefinition native, int difference, MethodBody body, ILProcessor il)
{
if (wrapper.Parameters.Count > 2)
{
// Todo: emit all parameters bar the last two
throw new NotImplementedException();
}
if (wrapper.ReturnType.Name != "Void")
{
if (difference == 2)
{
// Convert sized out-array/reference to return value, for example:
// void GenTextures(int n, int[] textures) -> int GenTexture()
// {
// const int n = 1;
// int buffers;
// calli GenTextures(n, &textures);
// return result;
// }
body.Variables.Add(new VariableDefinition(wrapper.ReturnType));
il.Emit(OpCodes.Ldc_I4, 1); // const int n = 1
il.Emit(OpCodes.Ldloca, body.Variables.Count - 1); // &buffers
}
else if (difference == 1)
{
// Convert unsized out-array/reference to return value, for example:
// void GetBoolean(GetPName pname, out bool data) -> bool GetBoolean(GetPName pname)
// {
// bool result;
// GetBooleanv(pname, &result);
// return result;
// }
body.Variables.Add(new VariableDefinition(wrapper.ReturnType));
EmitParameters(wrapper, native, body, il);
il.Emit(OpCodes.Ldloca, body.Variables.Count - 1);
}
else
{
Console.Error.WriteLine("Unknown wrapper type for ({0})", native.Name);
}
}
else
{
if (difference == 1)
{
// Convert in-array/reference to single element, for example:
// void DeleteTextures(int n, ref int textures) -> void DeleteTexture(int texture)
// {
// const int n = 1;
// calli DeleteTextures(n, &textures);
// }
il.Emit(OpCodes.Ldc_I4, 1); // const int n = 1
il.Emit(OpCodes.Ldarga, wrapper.Parameters.Last()); // &textures
}
else
{
Console.Error.WriteLine("Unknown wrapper type for ({0})", native.Name);
}
}
}
示例13: EmitParameters
static int EmitParameters(MethodDefinition method, MethodDefinition native, MethodBody body, ILProcessor il)
{
int i;
for (i = 0; i < method.Parameters.Count; i++)
{
var parameter = method.Parameters[i];
var p = method.Module.Import(method.Parameters[i].ParameterType);
il.Emit(OpCodes.Ldarg, i);
if (p.Name.Contains("Int32") && native.Parameters[i].ParameterType.Name.Contains("IntPtr"))
{
// This is a convenience Int32 overload for an IntPtr (size_t) parameter.
// We need to convert the loaded argument to IntPtr.
il.Emit(OpCodes.Conv_I);
}
else if (p.Name == "StringBuilder")
{
EmitStringBuilderParameter(method, parameter, body, il);
}
else if (p.Name == "String" && !p.IsArray)
{
EmitStringParameter(method, parameter, body, il);
}
else if (p.IsByReference)
{
body.Variables.Add(new VariableDefinition(new PinnedType(p)));
var index = body.Variables.Count - 1;
il.Emit(OpCodes.Stloc, index);
il.Emit(OpCodes.Ldloc, index);
il.Emit(OpCodes.Conv_I);
}
else if (p.IsArray)
{
if (p.Name != method.Module.Import(typeof(string[])).Name)
{
// .Net treats 1d arrays differently than higher rank arrays.
// 1d arrays are directly supported by instructions such as ldlen and ldelema.
// Higher rank arrays must be accessed through System.Array methods such as get_Length.
// 1d array:
// check array is not null
// check ldlen array > 0
// ldc.i4.0
// ldelema
// 2d array:
// check array is not null
// check array.get_Length() > 0
// ldc.i4.0
// ldc.i4.0
// call instance T& T[0..., 0...]::Address(int32, int32)
// Mono treats everything as a 1d array.
// Interestingly, the .Net approach works on both Mono and .Net.
// The Mono approach fails when using high-rank arrays on .Net.
// We should report a bug to http://bugzilla.xamarin.com
// Pin the array and pass the address
// of its first element.
var array = (ArrayType)p;
var element_type = p.GetElementType();
body.Variables.Add(new VariableDefinition(new PinnedType(new ByReferenceType(element_type))));
int pinned_index = body.Variables.Count - 1;
var empty = il.Create(OpCodes.Ldc_I4, 0);
var pin = il.Create(OpCodes.Ldarg, i);
var end = il.Create(OpCodes.Stloc, pinned_index);
// if (array == null) goto empty
il.Emit(OpCodes.Brfalse, empty);
// else if (array.Length != 0) goto pin
il.Emit(OpCodes.Ldarg, i);
if (array.Rank == 1)
{
il.Emit(OpCodes.Ldlen);
il.Emit(OpCodes.Conv_I4);
}
else
{
var get_length = method.Module.Import(
mscorlib.MainModule.GetType("System.Array").Methods.First(m => m.Name == "get_Length"));
il.Emit(OpCodes.Callvirt, get_length);
}
il.Emit(OpCodes.Brtrue, pin);
// empty: IntPtr ptr = IntPtr.Zero
il.Append(empty);
il.Emit(OpCodes.Conv_U);
il.Emit(OpCodes.Br, end);
// pin: &array[0]
il.Append(pin);
if (array.Rank == 1)
{
// 1d array (vector), address is taken by ldelema
il.Emit(OpCodes.Ldc_I4, 0);
il.Emit(OpCodes.Ldelema, element_type);
}
else
{
// 2d-3d array, address must be taken as follows:
// call instance T& T[0..., 0..., 0...]::Address(int, int, int)
//.........这里部分代码省略.........
示例14: EmitStringArrayParameter
static void EmitStringArrayParameter(MethodDefinition wrapper, ParameterDefinition parameter, MethodBody body, ILProcessor il)
{
var p = parameter.ParameterType;
// string[] masrhaling:
// IntPtr ptr = MarshalStringArrayToPtr(strings);
// try { calli }
// finally { FreeStringArrayPtr(ptr); }
var marshal_str_array_to_ptr = wrapper.Module.Import(TypeBindingsBase.Methods.First(m => m.Name == "MarshalStringArrayToPtr"));
// IntPtr ptr;
var variable_name = parameter.Name + "_string_array_ptr";
body.Variables.Add(new VariableDefinition(variable_name, TypeIntPtr));
int index = body.Variables.Count - 1;
// ptr = MarshalStringArrayToPtr(strings);
il.Emit(OpCodes.Call, marshal_str_array_to_ptr);
il.Emit(OpCodes.Stloc, index);
il.Emit(OpCodes.Ldloc, index);
// The finally block will be emitted in the function epilogue
}
示例15: EmitStringArrayEpilogue
static void EmitStringArrayEpilogue(MethodDefinition wrapper, ParameterDefinition parameter, MethodBody body, ILProcessor il)
{
// Note: only works for string vectors (1d arrays).
// We do not (and will probably never) support 2d or higher string arrays
var p = parameter.ParameterType;
var free = wrapper.Module.Import(TypeBindingsBase.Methods.First(m => m.Name == "FreeStringArrayPtr"));
// FreeStringArrayPtr(string_array_ptr, string_array.Length)
var variable_name = parameter.Name + "_string_array_ptr";
var v = body.Variables.First(m => m.Name == variable_name);
// load string_array_ptr
il.Emit(OpCodes.Ldloc, v.Index);
// load string_array.Length
il.Emit(OpCodes.Ldarg, parameter.Index);
il.Emit(OpCodes.Ldlen);
il.Emit(OpCodes.Conv_I4);
// call FreeStringArrayPtr
il.Emit(OpCodes.Call, free);
}