本文整理汇总了C#中Mono.Cecil.Cil.VariableDefinition类的典型用法代码示例。如果您正苦于以下问题:C# VariableDefinition类的具体用法?C# VariableDefinition怎么用?C# VariableDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
VariableDefinition类属于Mono.Cecil.Cil命名空间,在下文中一共展示了VariableDefinition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateTypeA
private TypeDefinition GenerateTypeA(AssemblyDefinition assembly)
{
var objType = assembly.MainModule.Import(typeof (object));
// define base class
var aType = new TypeDefinition("A", "", TypeAttributes.Class, objType);
assembly.MainModule.Types.Add(aType);
var typeParameter = new GenericParameter("T", aType);
aType.GenericParameters.Add(typeParameter);
var baseMethod = new MethodDefinition("Get",
MethodAttributes.Assem | MethodAttributes.Virtual |
MethodAttributes.NewSlot, typeParameter);
aType.Methods.Add(baseMethod);
baseMethod.DeclaringType = aType;
var local = new VariableDefinition(typeParameter);
baseMethod.Body.Variables.Add(local);
var cil = baseMethod.Body.CilWorker;
cil.Emit(OpCodes.Ldloca, local);
cil.Emit(OpCodes.Initobj, typeParameter);
cil.Emit(OpCodes.Ldloc, local);
cil.Emit(OpCodes.Ret);
return aType;
}
开发者ID:vestild,项目名称:nemerle,代码行数:27,代码来源:Cecil.GenericBaseClassAndOverrideOfNonPublicVirtualMethod.cs
示例2: InjectAsStreamReader
void InjectAsStreamReader(TypeDefinition targetType, FieldDefinition fieldDefinition)
{
AsStreamReaderMethod = new MethodDefinition("AsStreamReader", staticMethodAttributes, StreamReaderTypeReference);
var streamVariable = new VariableDefinition(StreamTypeReference);
AsStreamReaderMethod.Body.Variables.Add(streamVariable);
var pathParam = new ParameterDefinition(ModuleDefinition.TypeSystem.String);
AsStreamReaderMethod.Parameters.Add(pathParam);
AsStreamReaderMethod.Body.InitLocals = true;
var inst = AsStreamReaderMethod.Body.Instructions;
var skipReturn = Instruction.Create(OpCodes.Nop);
inst.Add(Instruction.Create(OpCodes.Ldsfld, fieldDefinition));
inst.Add(Instruction.Create(OpCodes.Ldarg, pathParam));
inst.Add(Instruction.Create(OpCodes.Callvirt, GetManifestResourceStreamMethod));
inst.Add(Instruction.Create(OpCodes.Stloc, streamVariable));
inst.Add(Instruction.Create(OpCodes.Ldloc, streamVariable));
inst.Add(Instruction.Create(OpCodes.Brtrue_S, skipReturn));
inst.Add(Instruction.Create(OpCodes.Ldnull));
inst.Add(Instruction.Create(OpCodes.Ret));
inst.Add(skipReturn);
inst.Add(Instruction.Create(OpCodes.Ldloc, streamVariable));
inst.Add(Instruction.Create(OpCodes.Newobj, StreamReaderConstructorReference));
inst.Add(Instruction.Create(OpCodes.Ret));
targetType.Methods.Add(AsStreamReaderMethod);
}
示例3: Visit
public void Visit(IBehaviorDefinition behaviorDefinition, CustomAttribute attribute)
{
ILProcessor processor = behaviorDefinition.Body.GetILProcessor();
var pingerDefinition = new VariableDefinition("pinger",
behaviorDefinition.Module.Import(pingType));
behaviorDefinition.Body.Variables.Add(pingerDefinition);
var start = new List<Instruction>
{
processor.Create(OpCodes.Ldstr, behaviorDefinition.Name),
processor.Create(OpCodes.Newobj,
behaviorDefinition.Module.Import(
pingType.GetConstructor(new[]{typeof(string)}))),
processor.Create(OpCodes.Stloc, pingerDefinition),
processor.Create(OpCodes.Ldloc, pingerDefinition),
processor.Create(OpCodes.Callvirt,
behaviorDefinition.Module.Import(pingType.GetMethod("Start",
new Type[] {}))),
};
var end = new List<Instruction>
{
processor.Create(OpCodes.Ldloc, pingerDefinition),
processor.Create(OpCodes.Callvirt,
behaviorDefinition.Module.Import(pingType.GetMethod("End",
new Type[] {})))
};
new InsertionAtStart(processor, behaviorDefinition).Run(start);
new InsertionAtEnd(processor, behaviorDefinition).Run(end);
foreach (var instruction in behaviorDefinition.Body.Instructions)
{
Console.Out.WriteLine("{0}/[{1}]: {2} {3}", instruction.Offset, Sequence(instruction.SequencePoint), instruction.OpCode, instruction.Operand);
}
}
示例4: 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);
}
示例5: Visit
public void Visit(IBehaviorDefinition behaviorDefinition, CustomAttribute attribute)
{
ILProcessor processor = behaviorDefinition.Body.GetILProcessor();
var pingerDefinition = new VariableDefinition("pinger",
behaviorDefinition.Module.Import(breakpointType));
behaviorDefinition.Body.Variables.Add(pingerDefinition);
var sequencedInstructions =
behaviorDefinition.Body.Instructions.Where(instruction => instruction.SequencePoint != null).ToList();
Console.Out.WriteLine(sequencedInstructions.Count);
// var extractedInstructions = Specific(sequencedInstructions, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 20, 21);
var extractedInstructions = Filtered(sequencedInstructions);
foreach (var instruction in extractedInstructions)
{
Type intType = typeof(int);
var breakPointInstructions = new List<Instruction>
{
processor.Create(OpCodes.Newobj, behaviorDefinition.Module.Import(
breakpointType.GetConstructor(new Type[]{}))),
processor.Create(OpCodes.Ldc_I4, instruction.SequencePoint.StartLine),
processor.Create(OpCodes.Ldc_I4, instruction.SequencePoint.StartColumn),
processor.Create(OpCodes.Ldc_I4, instruction.SequencePoint.EndLine),
processor.Create(OpCodes.Ldc_I4, instruction.SequencePoint.EndColumn),
processor.Create(OpCodes.Ldstr, instruction.SequencePoint.Document.Url),
processor.Create(OpCodes.Call,
behaviorDefinition.Module.Import(breakpointType.GetMethod("Activate",
new[] {intType, intType, intType, intType, typeof(string)})))
};
breakPointInstructions.ForEach(bpr => processor.InsertBefore(instruction, bpr));
foreach (var i in behaviorDefinition.Body.Instructions)
{
Console.Out.WriteLine("{0}/[{1}]: {2} {3}", i.Offset, Sequence(i.SequencePoint), i.OpCode, i.Operand);
}
}
}
示例6: AddBeforeAfterInvokerCall
int AddBeforeAfterInvokerCall(int index, PropertyDefinition property)
{
var beforeVariable = new VariableDefinition(msCoreReferenceFinder.ObjectTypeReference);
setMethodBody.Variables.Add(beforeVariable);
var afterVariable = new VariableDefinition(msCoreReferenceFinder.ObjectTypeReference);
setMethodBody.Variables.Add(afterVariable);
var isVirtual = property.GetMethod.IsVirtual;
var getMethod = property.GetMethod.GetGeneric();
index = instructions.Insert(index,
Instruction.Create(OpCodes.Ldarg_0),
CreateCall(getMethod),
//TODO: look into why this box is required
Instruction.Create(OpCodes.Box, property.GetMethod.ReturnType),
Instruction.Create(OpCodes.Stloc, afterVariable),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldstr, property.Name),
Instruction.Create(OpCodes.Ldloc, beforeVariable),
Instruction.Create(OpCodes.Ldloc, afterVariable),
CallEventInvoker()
);
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
CreateCall(getMethod),
//TODO: look into why this box is required
Instruction.Create(OpCodes.Box, property.GetMethod.ReturnType),
Instruction.Create(OpCodes.Stloc, beforeVariable));
return index + 4;
}
示例7: EmitBeforeInvoke
public EmitBeforeInvoke(VariableDefinition invocationInfo, VariableDefinition surroundingClassImplementation, VariableDefinition surroundingImplementation, Type registryType)
{
_invocationInfo = invocationInfo;
_surroundingClassImplementation = surroundingClassImplementation;
_surroundingImplementation = surroundingImplementation;
_registryType = registryType;
}
示例8: MethodBodyRewriterParameters
/// <summary>
/// Initializes a new instance of the <see cref="MethodBodyRewriterParameters"/> class.
/// </summary>
/// <param name="IL">The CilWorker that is responsible for the current method body.</param>
/// <param name="oldInstructions">The value indicating the list of old instructions in the current method body.</param>
/// <param name="interceptionDisabled">The value that determines whether or not interception is disabled.</param>
/// <param name="invocationInfo">The local variable that will store the <see cref="IInvocationInfo"/> instance.</param>
/// <param name="returnValue">The value indicating the local variable that will store the return value.</param>
/// <param name="methodReplacementProvider">The <see cref="IMethodReplacementProvider"/> instance.</param>
/// <param name="aroundInvokeProvider">The <see cref="IAroundInvokeProvider"/> instance.</param>
/// <param name="classMethodReplacementProvider">The class-level<see cref="IMethodReplacementProvider"/> instance.</param>
/// <param name="getMethodReplacementProviderMethod">The functor that resolves the GetMethodReplacementProvider method.</param>
/// <param name="registryType">The interception registry type that will be responsible for handling class-level interception events.</param>
public MethodBodyRewriterParameters(CilWorker IL, IEnumerable<Instruction> oldInstructions,
VariableDefinition interceptionDisabled,
VariableDefinition invocationInfo,
VariableDefinition returnValue,
VariableDefinition methodReplacementProvider,
VariableDefinition aroundInvokeProvider,
VariableDefinition classMethodReplacementProvider,
Func<ModuleDefinition, MethodReference> getMethodReplacementProviderMethod,
Type registryType)
{
if (methodReplacementProvider.VariableType.FullName != typeof(IMethodReplacementProvider).FullName)
throw new ArgumentException("methodReplacementProvider");
if (aroundInvokeProvider.VariableType.FullName != typeof(IAroundInvokeProvider).FullName)
throw new ArgumentException("aroundInvokeProvider");
_cilWorker = IL;
_oldInstructions = oldInstructions;
_interceptionDisabled = interceptionDisabled;
_invocationInfo = invocationInfo;
_returnValue = returnValue;
_methodReplacementProvider = methodReplacementProvider;
_aroundInvokeProvider = aroundInvokeProvider;
_classMethodReplacementProvider = classMethodReplacementProvider;
_getMethodReplacementProviderMethod = getMethodReplacementProviderMethod;
_registryType = registryType;
}
示例9: InjectInterceptedMethod
MethodDefinition InjectInterceptedMethod(TypeDefinition targetType, MethodDefinition innerOnPropertyChanging)
{
var delegateHolderInjector = new DelegateHolderInjector
{
TargetTypeDefinition=targetType,
OnPropertyChangingMethodReference = innerOnPropertyChanging,
ModuleWeaver = this
};
delegateHolderInjector.InjectDelegateHolder();
var method = new MethodDefinition(EventInvokerNames.First(), GetMethodAttributes(targetType), ModuleDefinition.TypeSystem.Void);
var propertyName = new ParameterDefinition("propertyName", ParameterAttributes.None, ModuleDefinition.TypeSystem.String);
method.Parameters.Add(propertyName);
if (InterceptorType == InvokerTypes.Before)
{
var before = new ParameterDefinition("before", ParameterAttributes.None, ModuleDefinition.TypeSystem.Object);
method.Parameters.Add(before);
}
var action = new VariableDefinition("firePropertyChanging", ActionTypeReference);
method.Body.Variables.Add(action);
var variableDefinition = new VariableDefinition("delegateHolder", delegateHolderInjector.TypeDefinition);
method.Body.Variables.Add(variableDefinition);
var instructions = method.Body.Instructions;
var last = Instruction.Create(OpCodes.Ret);
instructions.Add(Instruction.Create(OpCodes.Newobj, delegateHolderInjector.ConstructorDefinition));
instructions.Add(Instruction.Create(OpCodes.Stloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
instructions.Add(Instruction.Create(OpCodes.Stfld, delegateHolderInjector.PropertyName));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
instructions.Add(Instruction.Create(OpCodes.Stfld, delegateHolderInjector.Target));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldftn, delegateHolderInjector.MethodDefinition));
instructions.Add(Instruction.Create(OpCodes.Newobj, ActionConstructorReference));
instructions.Add(Instruction.Create(OpCodes.Stloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldfld, delegateHolderInjector.PropertyName));
if (InterceptorType == InvokerTypes.Before)
{
instructions.Add(Instruction.Create(OpCodes.Ldarg_2));
instructions.Add(Instruction.Create(OpCodes.Call, InterceptMethod));
}
else
{
instructions.Add(Instruction.Create(OpCodes.Call, InterceptMethod));
}
instructions.Add(last);
method.Body.InitLocals = true;
targetType.Methods.Add(method);
return method;
}
示例10: InjectMethod
MethodDefinition InjectMethod(TypeDefinition targetType, string eventInvokerName, FieldReference propertyChangingField)
{
var method = new MethodDefinition(eventInvokerName, GetMethodAttributes(targetType), ModuleDefinition.TypeSystem.Void);
method.Parameters.Add(new ParameterDefinition("propertyName", ParameterAttributes.None, ModuleDefinition.TypeSystem.String));
var handlerVariable = new VariableDefinition(PropChangingHandlerReference);
method.Body.Variables.Add(handlerVariable);
var boolVariable = new VariableDefinition(ModuleDefinition.TypeSystem.Boolean);
method.Body.Variables.Add(boolVariable);
var instructions = method.Body.Instructions;
var last = Instruction.Create(OpCodes.Ret);
instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
instructions.Add(Instruction.Create(OpCodes.Ldfld, propertyChangingField));
instructions.Add(Instruction.Create(OpCodes.Stloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldnull));
instructions.Add(Instruction.Create(OpCodes.Ceq));
instructions.Add(Instruction.Create(OpCodes.Stloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Brtrue_S, last));
instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
instructions.Add(Instruction.Create(OpCodes.Newobj, ComponentModelPropertyChangingEventConstructorReference));
instructions.Add(Instruction.Create(OpCodes.Callvirt, ComponentModelPropertyChangingEventHandlerInvokeReference));
instructions.Add(last);
method.Body.InitLocals = true;
targetType.Methods.Add(method);
return method;
}
示例11: CreateTestAssembly
private static AssemblyDefinition CreateTestAssembly(OpCode opCode, bool invert)
{
var name = new AssemblyNameDefinition("TestConditionalsBoundaryTurtle", new Version(1, 0));
var assembly = AssemblyDefinition.CreateAssembly(name, "TestClass", ModuleKind.Dll);
var type = new TypeDefinition("TestConditionalsBoundaryTurtle", "TestClass",
TypeAttributes.Class | TypeAttributes.Public);
var intType = assembly.MainModule.Import(typeof(int));
var boolType = assembly.MainModule.Import(typeof(bool));
var method = new MethodDefinition("TestMethod", MethodAttributes.Public, intType);
var param = new ParameterDefinition("input", ParameterAttributes.In, intType);
method.Parameters.Add(param);
var resultVariable = new VariableDefinition(boolType);
method.Body.Variables.Add(resultVariable);
var processor = method.Body.GetILProcessor();
Instruction loadReturnValueInstruction = processor.Create(OpCodes.Ldloc, resultVariable);
method.Body.Instructions.Add(processor.Create(OpCodes.Ldarg, param));
method.Body.Instructions.Add(processor.Create(OpCodes.Ldc_I4, 0));
method.Body.Instructions.Add(processor.Create(opCode));
if (invert)
{
method.Body.Instructions.Add(processor.Create(OpCodes.Ldc_I4, 0));
method.Body.Instructions.Add(processor.Create(OpCodes.Ceq));
}
method.Body.Instructions.Add(processor.Create(OpCodes.Stloc, resultVariable));
method.Body.Instructions.Add(loadReturnValueInstruction);
method.Body.Instructions.Add(processor.Create(OpCodes.Ret));
type.Methods.Add(method);
assembly.MainModule.Types.Add(type);
return assembly;
}
示例12: HandleDups
public static void HandleDups(MethodDefinition coroutine, OrderedDictionary<Instruction, List<Instruction>> coroutineInstructions, Instruction instruction, List<Instruction> current, TypeReference expectedType, ref VariableDefinition dupLoc)
{
// FIXME: This is kindof a kludge... think on it
if (instruction.Previous.OpCode != OpCodes.Dup)
return;
var action = current.ToArray ();
current.Clear ();
if (dupLoc == null) {
dupLoc = new VariableDefinition (coroutine.Module.Import (typeof (object)));
coroutine.Body.Variables.Add (dupLoc);
}
if (expectedType.IsValueType)
current.Add (Instruction.Create (OpCodes.Box, expectedType));
current.Add (Instruction.Create (OpCodes.Stloc, dupLoc));
current.AddRange (action);
current.Add (Instruction.Create (OpCodes.Ldloc, dupLoc));
if (expectedType.IsValueType) {
current.Add (Instruction.Create (OpCodes.Unbox, expectedType));
current.Add (Instruction.Create (OpCodes.Ldobj, expectedType));
}
}
示例13: GetMethodReplacementProvider
/// <summary>
/// Initializes a new instance of the <see cref="GetMethodReplacementProvider"/> class.
/// </summary>
/// <param name="methodReplacementProvider">The local variable that contains the <see cref="IMethodReplacementProvider"/> instance.</param>
/// <param name="hostMethod">The target method.</param>
/// <param name="resolveGetProviderMethod">The functor that will resolve the GetProvider method.</param>
public GetMethodReplacementProvider(VariableDefinition methodReplacementProvider, MethodDefinition hostMethod,
Func<ModuleDefinition, MethodReference> resolveGetProviderMethod)
{
_methodReplacementProvider = methodReplacementProvider;
_hostMethod = hostMethod;
_resolveGetProviderMethod = resolveGetProviderMethod;
}
示例14: AddV
public static VariableDefinition AddV(this Collection<VariableDefinition> variables,
TypeReference typeReference)
{
VariableDefinition variable = new VariableDefinition(typeReference);
variables.Add(variable);
return variable;
}
示例15: GetSurroundingClassImplementation
public GetSurroundingClassImplementation(VariableDefinition invocationInfo,
VariableDefinition surroundingClassImplementation, MethodInfo getSurroundingImplementationMethod)
{
_invocationInfo = invocationInfo;
_surroundingClassImplementation = surroundingClassImplementation;
_getSurroundingImplementationMethod = getSurroundingImplementationMethod;
}