本文整理汇总了C#中Mono.Cecil.ParameterDefinition类的典型用法代码示例。如果您正苦于以下问题:C# ParameterDefinition类的具体用法?C# ParameterDefinition怎么用?C# ParameterDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ParameterDefinition类属于Mono.Cecil命名空间,在下文中一共展示了ParameterDefinition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddConstructor
public void AddConstructor(TypeDefinition adapterType,
FieldReference targetField)
{
var adapterCtor = adapterType.AddDefaultConstructor();
var adapterParameter = new ParameterDefinition(_targetDependency);
adapterCtor.Parameters.Add(adapterParameter);
// HACK: Remove the ret instruction from the default constructor and replace it with
// the field setter
var adapterBody = adapterCtor.Body;
var adapterInstructions = adapterBody.Instructions.Cast<Instruction>().Where(i => i.OpCode != OpCodes.Ret).ToArray();
adapterBody.Instructions.Clear();
// Copy the old instructions
var IL = adapterBody.CilWorker;
foreach (var instruction in adapterInstructions)
{
IL.Append(instruction);
}
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldarg, adapterParameter);
IL.Emit(OpCodes.Stfld, targetField);
IL.Emit(OpCodes.Ret);
}
示例2: MakeHostInstanceGeneric
internal static MethodReference MakeHostInstanceGeneric(this MethodReference self,
params TypeReference[] arguments)
{
var genericInstanceType = self.DeclaringType.MakeGenericInstanceType(arguments);
var reference = new MethodReference(self.Name, self.ReturnType, genericInstanceType)
{
HasThis = self.HasThis,
ExplicitThis = self.ExplicitThis,
CallingConvention = self.CallingConvention
};
foreach (var parameter in self.Parameters)
{
// TODO: This won't work with methods that has generic arguments, since the ParameterType needs to be replaced. @asbjornu
var parameterDefinition = new ParameterDefinition(parameter.Name,
parameter.Attributes,
parameter.ParameterType);
reference.Parameters.Add(parameterDefinition);
}
foreach (var genericParameter in self.GenericParameters)
reference.GenericParameters.Add(new GenericParameter(genericParameter.Name, reference));
return reference;
}
示例3: ArgumentReferenceEmitter
internal ArgumentReferenceEmitter(IdentifierExpression identifierExpression, ParameterDefinition parameterDefinition, ILGenerator ilGenerator, IOpCodeIndexer instructionsIndexer)
: base(ilGenerator, instructionsIndexer) {
_parameterDefinition = parameterDefinition;
_identifierExpression = identifierExpression;
Type = parameterDefinition.ParameterType.GetActualType();
}
示例4: 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;
}
示例5: 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;
}
示例6: UseParameter
private bool UseParameter (MethodDefinition method, ParameterDefinition parameter)
{
foreach (Instruction instruction in method.Body.Instructions) {
switch (instruction.OpCode.Code) {
case Code.Ldarg_0:
if (method.Parameters.IndexOf (parameter) == 0)
return true;
break;
case Code.Ldarg_1:
if (method.Parameters.IndexOf (parameter) == (method.IsStatic? 1 : 0))
return true;
break;
case Code.Ldarg_2:
if (method.Parameters.IndexOf (parameter) == (method.IsStatic? 2 : 1))
return true;
break;
case Code.Ldarg_3:
if (method.Parameters.IndexOf (parameter) == (method.IsStatic? 3 : 2))
return true;
break;
case Code.Ldarg_S:
if (instruction.Operand.Equals (parameter))
return true;
break;
default:
break;
}
}
return false;
}
示例7: 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);
}
示例8: DomCecilParameter
public DomCecilParameter (ParameterDefinition parameterDefinition)
{
this.parameterDefinition = parameterDefinition;
base.Name = parameterDefinition.Name;
// base.modifiers = DomCecilType.GetModifiers (parameterDefinition..Attributes);
base.ReturnType = DomCecilMethod.GetReturnType (parameterDefinition.ParameterType);
if (parameterDefinition.ParameterType is Mono.Cecil.ReferenceType) {
this.ParameterModifiers = (parameterDefinition.Attributes & ParameterAttributes.Out) == ParameterAttributes.Out ? ParameterModifiers.Out : ParameterModifiers.Ref;
} else {
this.ParameterModifiers = ParameterModifiers.In;
}
if ((parameterDefinition.Attributes & ParameterAttributes.Optional) == ParameterAttributes.Optional)
this.ParameterModifiers |= ParameterModifiers.Optional;
if ((parameterDefinition.Attributes & ParameterAttributes.HasDefault) == ParameterAttributes.HasDefault) {
this.DefaultValue = new System.CodeDom.CodePrimitiveExpression (parameterDefinition.Constant);
}
if (ReturnType.ArrayDimensions > 0) {
foreach (CustomAttribute customAttribute in parameterDefinition.CustomAttributes) {
if (customAttribute.Constructor.DeclaringType.FullName == "System.ParamArrayAttribute")
this.ParameterModifiers |= ParameterModifiers.Params;
}
}
}
示例9: Parameter
public Parameter(ParameterDefinition parameterDefinition, ComponentCache componentCache) : base(parameterDefinition.ParameterType, componentCache)
{
Contract.Requires<ArgumentNullException>(parameterDefinition != null);
parameter = parameterDefinition;
cache = componentCache;
}
示例10: CreateTestAssembly
private static AssemblyDefinition CreateTestAssembly()
{
var name = new AssemblyNameDefinition("TestBranchConditionTurtle", new Version(1, 0));
var assembly = AssemblyDefinition.CreateAssembly(name, "TestClass", ModuleKind.Dll);
var type = new TypeDefinition("TestBranchConditionTurtle", "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 leftParam = new ParameterDefinition("left", ParameterAttributes.In, intType);
var rightParam = new ParameterDefinition("right", ParameterAttributes.In, intType);
method.Parameters.Add(leftParam);
method.Parameters.Add(rightParam);
var resultVariable = new VariableDefinition(boolType);
method.Body.Variables.Add(resultVariable);
var processor = method.Body.GetILProcessor();
Instruction loadReturnValueInstruction = processor.Create(OpCodes.Ldloc, resultVariable);
Instruction storeTrueInReturnValueInstruction = processor.Create(OpCodes.Ldc_I4, -1);
method.Body.Instructions.Add(processor.Create(OpCodes.Ldarg, leftParam));
method.Body.Instructions.Add(processor.Create(OpCodes.Ldarg, rightParam));
method.Body.Instructions.Add(processor.Create(OpCodes.Ceq));
method.Body.Instructions.Add(processor.Create(OpCodes.Brtrue_S, storeTrueInReturnValueInstruction));
method.Body.Instructions.Add(processor.Create(OpCodes.Ldc_I4, 0));
method.Body.Instructions.Add(processor.Create(OpCodes.Stloc, resultVariable));
method.Body.Instructions.Add(processor.Create(OpCodes.Br_S, loadReturnValueInstruction));
method.Body.Instructions.Add(storeTrueInReturnValueInstruction);
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;
}
示例11: CheckForExistingGuard
private static bool CheckForExistingGuard(Collection<Instruction> instructions, ParameterDefinition parameter)
{
for (var i = 1; i < instructions.Count - 1; i++)
{
if (instructions[i].OpCode == OpCodes.Newobj)
{
var newObjectMethodRef = instructions[i].Operand as MethodReference;
if (newObjectMethodRef == null || instructions[i + 1].OpCode != OpCodes.Throw)
continue;
// Checks for throw new ArgumentNullException("x");
if (newObjectMethodRef.FullName == ReferenceFinder.ArgumentNullExceptionConstructor.FullName &&
instructions[i - 1].OpCode == OpCodes.Ldstr &&
(string)(instructions[i - 1].Operand) == parameter.Name)
return true;
// Checks for throw new ArgumentNullException("x", "some message");
if (newObjectMethodRef.FullName == ReferenceFinder.ArgumentNullExceptionWithMessageConstructor.FullName &&
i > 1 &&
instructions[i - 2].OpCode == OpCodes.Ldstr &&
(string)(instructions[i - 2].Operand) == parameter.Name)
return true;
}
}
return false;
}
示例12: LoadArgumentOntoStack
public static void LoadArgumentOntoStack(List<Instruction> instructions, ParameterDefinition parameter)
{
// Load the argument onto the stack
instructions.Add(Instruction.Create(OpCodes.Ldarg, parameter));
var elementType = parameter.ParameterType.GetElementType();
if (parameter.ParameterType.IsByReference)
{
if (elementType.IsGenericParameter)
{
// Loads an object reference onto the stack
instructions.Add(Instruction.Create(OpCodes.Ldobj, elementType));
// Box the type to an object
instructions.Add(Instruction.Create(OpCodes.Box, elementType));
}
else
{
// Loads an object reference onto the stack
instructions.Add(Instruction.Create(OpCodes.Ldind_Ref));
}
}
else if (elementType.IsGenericParameter)
{
// Box the type to an object
instructions.Add(Instruction.Create(OpCodes.Box, parameter.ParameterType));
}
}
示例13: CreateTestAssembly
private static AssemblyDefinition CreateTestAssembly()
{
var name = new AssemblyNameDefinition("TestArithmeticOperatorTurtleAdd", new Version(1, 0));
var assembly = AssemblyDefinition.CreateAssembly(name, "TestClass", ModuleKind.Dll);
var type = new TypeDefinition("TestArithmeticOperatorTurtleAdd", "TestClass",
TypeAttributes.Class | TypeAttributes.Public);
var intType = assembly.MainModule.Import(typeof(int));
var method = new MethodDefinition("TestMethod", MethodAttributes.Public, intType);
var leftParam = new ParameterDefinition("left", ParameterAttributes.In, intType);
var rightParam = new ParameterDefinition("right", ParameterAttributes.In, intType);
method.Parameters.Add(leftParam);
method.Parameters.Add(rightParam);
var resultVariable = new VariableDefinition(intType);
method.Body.Variables.Add(resultVariable);
var processor = method.Body.GetILProcessor();
method.Body.Instructions.Add(processor.Create(OpCodes.Ldarg_1));
method.Body.Instructions.Add(processor.Create(OpCodes.Ldarg_2));
method.Body.Instructions.Add(processor.Create(OpCodes.Add));
method.Body.Instructions.Add(processor.Create(OpCodes.Stloc, resultVariable));
method.Body.Instructions.Add(processor.Create(OpCodes.Ldloc, resultVariable));
method.Body.Instructions.Add(processor.Create(OpCodes.Ret));
type.Methods.Add(method);
assembly.MainModule.Types.Add(type);
return assembly;
}
示例14: ImplementISetThis
/// <summary>
/// Implement IAsyncSetThis.
/// </summary>
private static void ImplementISetThis(TypeDefinition type, ReachableContext reachableContext)
{
var thisField = type.Fields.FirstOrDefault(x => x.Name.StartsWith("<>") && x.Name.EndsWith("__this"));
if (thisField == null)
return;
// Add interface
var intfType = type.Module.Import(new TypeReference(InternalConstants.Dot42InternalNamespace, "IAsyncSetThis", type.Module, type.Module.Assembly.Name));
type.Interfaces.Add(new InterfaceImpl(intfType));
// Add "SetThis(object)" method
var method = new MethodDefinition("SetThis", MethodAttributes.Public, type.Module.TypeSystem.Void);
method.SetReachable(reachableContext);
type.Methods.Add(method);
var valueParam = new ParameterDefinition(type.Module.TypeSystem.Object);
method.Parameters.Add(valueParam);
method.Body = new MethodBody(method) { InitLocals = true };
var seq = new ILSequence();
seq.Emit(OpCodes.Ldarg_0); // this
seq.Emit(OpCodes.Ldarg, valueParam);
seq.Emit(OpCodes.Castclass, thisField.FieldType);
seq.Emit(OpCodes.Stfld, thisField);
seq.Emit(OpCodes.Ret);
seq.AppendTo(method.Body);
}
示例15: Execute
public static MethodDefinition Execute(PropertyDefinition property_definition, MethodDefinition notify_method, DependencyMap map)
{
log.Trace("\t\t\t\t\tAdding collection notification method for " + property_definition.Name);
// Create method
TypeReference return_type = property_definition.Module.Import(typeof (void));
MethodDefinition collection_notification = new MethodDefinition(property_definition.Name + "CollectionNotification", Mono.Cecil.MethodAttributes.Private | Mono.Cecil.MethodAttributes.HideBySig, return_type);
// Add parameters
TypeReference sender_type = property_definition.Module.Import(typeof(object));
TypeReference args_type = property_definition.Module.Import(typeof(NotifyCollectionChangedEventArgs));
ParameterDefinition sender = new ParameterDefinition("sender", Mono.Cecil.ParameterAttributes.None, sender_type);
ParameterDefinition args = new ParameterDefinition("args", Mono.Cecil.ParameterAttributes.None, args_type);
collection_notification.Parameters.Add(sender);
collection_notification.Parameters.Add(args);
// Add notifications for dependent properties
ILProcessor processor = collection_notification.Body.GetILProcessor();
foreach (var target in map.GetDependenciesFor(property_definition.Name))
{
log.Trace("\t\t\t\t\t\tAdding dependency " + target);
processor.Emit(OpCodes.Ldarg_0);
processor.Emit(OpCodes.Ldstr, target);
processor.Emit(OpCodes.Call, notify_method);
}
processor.Emit(OpCodes.Ret);
// Add method to class
property_definition.DeclaringType.Methods.Add(collection_notification);
return collection_notification;
}