本文整理汇总了C#中MethodDefinition类的典型用法代码示例。如果您正苦于以下问题:C# MethodDefinition类的具体用法?C# MethodDefinition怎么用?C# MethodDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MethodDefinition类属于命名空间,在下文中一共展示了MethodDefinition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessMethod
void ProcessMethod(MethodDefinition method)
{
if (method == null)
{
return;
}
if (method.IsFinal)
{
return;
}
if (method.IsStatic)
{
return;
}
if (method.IsVirtual)
{
return;
}
if (method.IsPrivate)
{
return;
}
method.IsVirtual = true;
method.IsNewSlot = true;
}
示例2: ProcessMethod
void ProcessMethod(MethodDefinition method)
{
var asyncAttribute = method.CustomAttributes.FirstOrDefault(_ => _.AttributeType.Name == "AsyncStateMachineAttribute");
if (asyncAttribute == null)
{
var methodProcessor = new MethodProcessor
{
ModuleWeaver = this,
TypeSystem = ModuleDefinition.TypeSystem,
Method = method,
};
methodProcessor.InnerProcess();
return;
}
LogError(string.Format("Could not process '{0}'. async methods are not currently supported. Feel free to submit a pull request if you want this feature.", method.FullName));
//var fullName = method.FullName;
//var customAttributeArgument = asyncAttribute.ConstructorArguments.First();
//var typeReference = (TypeReference) customAttributeArgument.Value;
//var asyncTypeDefinition = typeReference.Resolve();
//var methodProcessorAsync = new MethodProcessorAsync
// {
// ModuleWeaver = this,
// TypeSystem = ModuleDefinition.TypeSystem,
// AsyncTypeReference = asyncTypeDefinition,
// OriginalMethod = method
// };
//methodProcessorAsync.Process();
}
示例3: AddGetItem
void AddGetItem()
{
var method = new MethodDefinition(DataErrorInfoFinder.InterfaceRef.FullName + ".get_Item", MethodAttributes, TypeSystem.String)
{
IsGetter = true,
IsPrivate = true,
SemanticsAttributes = MethodSemanticsAttributes.Getter,
};
method.Overrides.Add(DataErrorInfoFinder.GetItemMethod);
method.Parameters.Add(new ParameterDefinition(TypeSystem.String));
method.Body.Instructions.Append(
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldfld, ValidationTemplateField),
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Callvirt, DataErrorInfoFinder.GetItemMethod),
Instruction.Create(OpCodes.Ret));
var property = new PropertyDefinition(DataErrorInfoFinder.InterfaceRef.FullName + ".Item", PropertyAttributes.None, TypeSystem.String)
{
GetMethod = method,
};
TypeDefinition.Methods.Add(method);
TypeDefinition.Properties.Add(property);
}
示例4: FindIsChangedEventInvokerMethodDefinition
bool FindIsChangedEventInvokerMethodDefinition(TypeDefinition type, out MethodDefinition methodDefinition)
{
//todo: check bool type
methodDefinition = null;
var propertyDefinition = type.Properties
.FirstOrDefault(x =>
x.Name == isChangedPropertyName &&
x.SetMethod != null &&
x.SetMethod.IsPublic
);
if (propertyDefinition != null)
{
if (propertyDefinition.PropertyType.FullName != ModuleDefinition.TypeSystem.Boolean.FullName)
{
LogWarning(string.Format("Found '{0}' but is was of type '{1}' instead of '{2}' so it will not be used.", propertyDefinition.GetName(), propertyDefinition.PropertyType.Name, ModuleDefinition.TypeSystem.Boolean.Name));
return false;
}
if (propertyDefinition.SetMethod.IsStatic)
{
throw new WeavingException(string.Format("Found '{0}' but is was static. Change it to non static.", propertyDefinition.GetName()));
}
methodDefinition = propertyDefinition.SetMethod;
}
return methodDefinition != null;
}
示例5: Process
public void Process(MethodDefinition method)
{
method.Body.SimplifyMacros();
var instructions = method.Body.Instructions;
for (var index = 0; index < instructions.Count; index++)
{
var line = instructions[index];
if (line.OpCode != OpCodes.Call)
{
continue;
}
var methodReference = line.Operand as MethodReference;
if (methodReference == null)
{
continue;
}
if (IsSetExceptionMethod(methodReference))
{
var previous = instructions[index-1];
instructions.Insert(index, Instruction.Create(OpCodes.Call, HandleMethodFinder.HandleMethod));
index++;
instructions.Insert(index, Instruction.Create(previous.OpCode, (VariableDefinition)previous.Operand));
index++;
}
}
method.Body.OptimizeMacros();
}
示例6: CloneMethodWithDeclaringType
private static MethodReference CloneMethodWithDeclaringType(MethodDefinition methodDef, TypeReference declaringTypeRef)
{
if (!declaringTypeRef.IsGenericInstance || methodDef == null)
{
return methodDef;
}
var methodRef = new MethodReference(methodDef.Name, methodDef.ReturnType, declaringTypeRef)
{
CallingConvention = methodDef.CallingConvention,
HasThis = methodDef.HasThis,
ExplicitThis = methodDef.ExplicitThis
};
foreach (var paramDef in methodDef.Parameters)
{
methodRef.Parameters.Add(new ParameterDefinition(paramDef.Name, paramDef.Attributes, paramDef.ParameterType));
}
foreach (var genParamDef in methodDef.GenericParameters)
{
methodRef.GenericParameters.Add(new GenericParameter(genParamDef.Name, methodRef));
}
return methodRef;
}
示例7: CodeReader
public CodeReader (MethodDefinition method, MetadataReader reader)
: base (reader.image.Stream)
{
this.reader = reader;
this.reader.context = method;
this.Position = (int) reader.image.ResolveVirtualAddress ((uint) method.RVA);
}
示例8: CreateDefaultConstructor
/// <summary>
/// Creates the default constructor.
/// </summary>
public MethodDefinition CreateDefaultConstructor(TypeDefinition type)
{
_logInfo("AddDefaultConstructor() " + type.Name);
// Create method for a constructor
var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
var method = new MethodDefinition(".ctor", methodAttributes, _moduleDefinition.TypeSystem.Void);
// Set parameters for constructor, and add corresponding assignment in method body:
// 1. Add each property in the class as a parameter for the constructor
// 2. Set the properties in the class from the constructor parameters
// Properties marked with the [ImmutableClassIgnore] attribute are ignored
foreach (var prop in type.Properties)
{
if (prop.CustomAttributes.ContainsAttribute("ImmutableClassIgnoreAttribute"))
{
_logInfo("Ignoring property " + prop.Name);
continue;
}
string paramName = Char.ToLowerInvariant(prop.Name [0]) + prop.Name.Substring (1); // convert first letter of name to lowercase
_logInfo("Adding parameter " + paramName + " to ctor");
var pd = (new ParameterDefinition(paramName, ParameterAttributes.HasDefault, prop.PropertyType));
method.Parameters.Add(pd);
_logInfo("Setting property " + prop.Name);
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, pd));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, prop.SetMethod));
}
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
return method;
}
示例9: PInvokeInfo
public PInvokeInfo (MethodDefinition meth, PInvokeAttributes attrs,
string entryPoint, ModuleReference mod) : this (meth)
{
m_attributes = attrs;
m_entryPoint = entryPoint;
m_module = mod;
}
示例10: IsAssertMethod
public bool IsAssertMethod(MethodDefinition method, out int usefulParameters)
{
SafeDebug.AssumeNotNull(method, "method");
TypeDefinition type;
if (method.TryGetDeclaringType(out type))
{
if (type.SerializableName == NUnitTestFrameworkMetadata.AssertTypeDefinition)
{
switch (method.ShortName)
{
case "IsEmpty":
case "IsNotEmpty":
case "False":
case "IsFalse":
case "True":
case "IsTrue":
case "IsAssignableFrom":
case "IsNotAssignableFrom":
case "IsNull":
case "IsNotNull":
case "Null":
case "NotNull":
case "IsNotNullOrEmpty":
case "IsNullOrEmpty":
case "IsNan":
case "IsInstanceOf": //not entirely correct
case "IsNotInstanceOf": //not entirely correct
usefulParameters = 1;
return true;
case "AreEqual":
case "AssertDoublesAreEqual":
case "AreNotEqual":
case "Contains":
case "AreSame":
case "AreNotSame":
case "Greater":
case "GreaterOrEqual":
case "Less":
case "LessOrEqual":
usefulParameters = 2;
return true;
}
}
else if (type.SerializableName == NUnitTestFrameworkMetadata.CollectionAssertTypeDefinition)
{
switch (method.ShortName)
{
case "Equals":
case "ReferenceEquals":
usefulParameters = -1;
return false;
default:
usefulParameters = 0;
return true;
}
}
}
usefulParameters = -1;
return false;
}
示例11: IsSecurityCritical
public bool IsSecurityCritical (MethodDefinition method)
{
string entry = method.GetFullName ();
if (critical.Contains (entry))
return true;
TypeDefinition type = method.DeclaringType;
if (!IsSecurityCritical (type))
return false;
switch (method.Name) {
// e.g. everything we override from System.Object (which is transparent)
case "Equals":
return (method.Parameters.Count != 1 || method.Parameters [0].ParameterType.FullName != "System.Object");
case "Finalize":
case "GetHashCode":
case "ToString":
return method.HasParameters;
// e.g. some transparent interfaces, like IDisposable (implicit or explicit)
// downgrade some SC into SSC to respect the override/inheritance rules
case "System.IDisposable.Dispose":
case "Dispose":
return method.HasParameters;
default:
return true;
}
}
示例12: CreateDisposeBoolMethod
void CreateDisposeBoolMethod()
{
DisposeBoolMethod = new MethodDefinition("Dispose", MethodAttributes.HideBySig | MethodAttributes.Private, typeSystem.Void);
var disposingParameter = new ParameterDefinition("disposing", ParameterAttributes.None, typeSystem.Boolean);
DisposeBoolMethod.Parameters.Add(disposingParameter);
var instructions = DisposeBoolMethod.Body.Instructions;
instructions.Add(TypeProcessor.GetDisposeEscapeInstructions());
var skipDisposeManaged = Instruction.Create(OpCodes.Nop);
instructions.Add(
Instruction.Create(OpCodes.Ldarg, disposingParameter),
Instruction.Create(OpCodes.Brfalse, skipDisposeManaged),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Call, DisposeManagedMethod),
skipDisposeManaged,
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Call, DisposeUnmanagedMethod)
);
instructions.Add(TypeProcessor.GetDisposedInstructions());
instructions.Add(Instruction.Create(OpCodes.Ret));
TypeProcessor.TargetType.Methods.Add(DisposeBoolMethod);
}
示例13: GetSingleField
static FieldDefinition GetSingleField(PropertyDefinition property, Code code, MethodDefinition methodDefinition)
{
if (methodDefinition?.Body == null)
{
return null;
}
FieldReference fieldReference = null;
foreach (var instruction in methodDefinition.Body.Instructions)
{
if (instruction.OpCode.Code == code)
{
//if fieldReference is not null then we are at the second one
if (fieldReference != null)
{
return null;
}
var field = instruction.Operand as FieldReference;
if (field != null)
{
if (field.DeclaringType != property.DeclaringType)
{
continue;
}
if (field.FieldType != property.PropertyType)
{
continue;
}
fieldReference = field;
}
}
}
return fieldReference?.Resolve();
}
示例14: Parse
public static void Parse (MethodDefinition method, IILVisitor visitor)
{
if (method == null)
throw new ArgumentNullException ("method");
if (visitor == null)
throw new ArgumentNullException ("visitor");
if (!method.HasBody || !method.HasImage)
throw new ArgumentException ();
var context = CreateContext (method, visitor);
var code = context.Code;
code.MoveTo (method.RVA);
var flags = code.ReadByte ();
switch (flags & 0x3) {
case 0x2: // tiny
int code_size = flags >> 2;
ParseCode (code_size, context);
break;
case 0x3: // fat
code.position--;
ParseFatMethod (context);
break;
default:
throw new NotSupportedException ();
}
}
示例15: CodeReader
/// <summary>
/// Default ctor
/// </summary>
internal CodeReader(MethodDefinition method, CodeAttribute codeAttribute, byte[] code, ConstantPool cp)
{
this.method = method;
this.codeAttribute = codeAttribute;
this.code = code;
this.cp = cp;
}