Class MethodDefinition
- Namespace
- AsmResolver.DotNet
- Assembly
- AsmResolver.DotNet.dll
Represents a single method in a type definition of a .NET module.
public class MethodDefinition : MetadataMember, IMemberDefinition, IOwnedCollectionElement<TypeDefinition>, IMemberRefParent, ICustomAttributeType, IMethodDefOrRef, IMethodDescriptor, IHasCustomAttribute, IHasGenericParameters, IMemberForwarded, IMemberDescriptor, IFullNameProvider, INameProvider, IModuleProvider, IImportable, IHasSecurityDeclaration, IManagedEntryPoint, IMetadataMember
- Inheritance
-
MethodDefinition
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
MethodDefinition(MetadataToken)
Initializes a new method definition.
protected MethodDefinition(MetadataToken token)
Parameters
token
MetadataTokenThe token of the method
MethodDefinition(Utf8String?, MethodAttributes, MethodSignature?)
Creates a new method definition.
public MethodDefinition(Utf8String? name, MethodAttributes attributes, MethodSignature? signature)
Parameters
name
Utf8StringThe name of the method.
attributes
MethodAttributesThe attributes.
signature
MethodSignatureThe signature of the method
Remarks
For a valid .NET image, if HasThis of the signature referenced by
signature
is set, the Static bit should be unset in
attributes
and vice versa.
Properties
Attributes
Gets or sets the attributes associated to the method.
public MethodAttributes Attributes { get; set; }
Property Value
CheckAccessOnOverride
Gets or sets a value indicating the method can only be overridden if it is also accessible.
public bool CheckAccessOnOverride { get; set; }
Property Value
CilMethodBody
Gets or sets the managed CIL body of the method if available.
public CilMethodBody? CilMethodBody { get; set; }
Property Value
Remarks
If this property is set to null
, it does not necessarily mean the method does not have a method body.
There could be an unmanaged method body assigned instead. See the MethodBody or
HasMethodBody properties instead.
Updating this property does not automatically set the appropriate implementation attributes in the ImplAttributes.
CustomAttributes
Gets a collection of custom attributes assigned to this member.
public IList<CustomAttribute> CustomAttributes { get; }
Property Value
DeclaringType
Gets the type that defines the method.
public TypeDefinition? DeclaringType { get; set; }
Property Value
ExportInfo
Gets or sets the unmanaged export info assigned to this method (if available). This can be used to indicate that a method needs to be exported in the final PE file as an unmanaged symbol.
public UnmanagedExportInfo? ExportInfo { get; set; }
Property Value
FullName
Gets the full name of the object.
public string FullName { get; }
Property Value
GenericParameters
Gets a collection of generic parameters this member defines.
public IList<GenericParameter> GenericParameters { get; }
Property Value
HasMethodBody
Gets a value indicating whether the method is implemented using a method body. That is, whether the
MethodBody property is not null
.
public bool HasMethodBody { get; }
Property Value
HasSecurity
Gets or sets a value indicating the method has security attributes assigned to it.
public bool HasSecurity { get; set; }
Property Value
Remarks
Methods containing security attributes should have this flag set. This property does not automatically however when attributes are added or removed from this method, nor does it reflect the existence of attributes.
HasSecurityMitigations
Gets or sets a value indicating that the JIT compiler should look for security mitigation attributes,
such as the user-defined System.Runtime.CompilerServices.SecurityMitigationsAttribute
. If found,
the JIT compiler applies any related security mitigations. Available starting with .NET Framework 4.8.
public bool HasSecurityMitigations { get; set; }
Property Value
Remarks
This is an undocumented flag and is currently not used: Original addition: https://github.com/dotnet/dotnet-api-docs/pull/2253 Documentation removal: https://github.com/dotnet/dotnet-api-docs/pull/4652
ImplAttributes
Gets or sets the attributes that describe the implementation of the method body.
public MethodImplAttributes ImplAttributes { get; set; }
Property Value
ImplementationMap
Gets or sets the Platform Invoke (P/Invoke) implementation mapping of this member (if available).
public ImplementationMap? ImplementationMap { get; set; }
Property Value
IsAbstract
Gets or sets a value indicating the method is marked abstract, and should be overridden by a derived class.
public bool IsAbstract { get; set; }
Property Value
Remarks
Methods with this flag set should not have a method body assigned for a valid .NET executable. However, updating this flag will not remove the body of this method, nor does the existence of the method body reflect the value of this property.
IsAddMethod
Gets a value indicating whether the method is an add method for an event.
public bool IsAddMethod { get; }
Property Value
IsAggressiveInlining
Gets or sets a value indicating whether the method should be inlined if possible.
public bool IsAggressiveInlining { get; set; }
Property Value
IsAggressiveOptimization
Gets or sets a value indicating whether the method contains hot code and should be aggressively optimized.
public bool IsAggressiveOptimization { get; set; }
Property Value
IsAssembly
Gets or sets a value indicating whether the method is marked private and can only be accessed by members within the same assembly.
public bool IsAssembly { get; set; }
Property Value
IsCompilerControlled
Gets or sets a value indicating whether the method is compiler controlled and cannot be referenced directly.
public bool IsCompilerControlled { get; set; }
Property Value
IsConstructor
Gets a value indicating whether the method is a (class) constructor.
public bool IsConstructor { get; }
Property Value
IsFamily
Gets or sets a value indicating whether the method is marked private and can only be accessed by members within the same enclosing type, as well as any derived type.
public bool IsFamily { get; set; }
Property Value
IsFamilyAndAssembly
Gets or sets a value indicating whether the method is marked family and assembly, and can only be accessed by members within the same enclosing type and any derived type, within the same assembly.
public bool IsFamilyAndAssembly { get; set; }
Property Value
IsFamilyOrAssembly
Gets or sets a value indicating whether the method is marked family or assembly, and can only be accessed by members within the same enclosing type and any derived type, or within the same assembly.
public bool IsFamilyOrAssembly { get; set; }
Property Value
IsFinal
Gets or sets a value indicating whether the method is marked final and cannot be overridden by a derived class.
public bool IsFinal { get; set; }
Property Value
IsFireMethod
Gets a value indicating whether the method is a fire method for an event.
public bool IsFireMethod { get; }
Property Value
IsForwardReference
Gets or sets a value indicating whether the method body is forwarded.
public bool IsForwardReference { get; set; }
Property Value
IsGetMethod
Gets a value indicating whether the method is a get method for a property.
public bool IsGetMethod { get; }
Property Value
IsHideBySig
Gets or sets a value indicating whether the method is distinguished by both its name and signature.
public bool IsHideBySig { get; set; }
Property Value
IsIL
Gets or sets a value indicating the method body is implemented using the Common Intermediate Language (CIL).
public bool IsIL { get; set; }
Property Value
IsInternalCall
Gets or sets a value indicating whether the method is an internal call into the runtime.
public bool IsInternalCall { get; set; }
Property Value
IsNative
Gets or sets a value indicating the method body is implemented using the Common Intermediate Language (CIL).
public bool IsNative { get; set; }
Property Value
IsNewSlot
Gets or sets a value indicating whether the runtime allocate a new slot in the VTable of the enclosing class for this method.
public bool IsNewSlot { get; set; }
Property Value
IsNoOptimization
Gets or sets a value indicating whether the runtime should not optimize the code upon generating native code.
public bool IsNoOptimization { get; set; }
Property Value
IsOPTIL
Gets or sets a value indicating the method body is implemented using OPTIL.
public bool IsOPTIL { get; set; }
Property Value
IsPInvokeImpl
Gets or sets a value indicating the method contains Platform Invoke information.
public bool IsPInvokeImpl { get; set; }
Property Value
Remarks
Methods containing Platform Invoke information should have this flag set. This property does not update automatically however when P/Invoke information is assigned to this method, nor does it reflect the existence of P/Invoke information.
IsPrivate
Gets or sets a value indicating whether the method is marked private and can only be accessed by members within the same enclosing type.
public bool IsPrivate { get; set; }
Property Value
IsPublic
Gets or sets a value indicating whether the method is marked public, and can be accessed by any member having access to the enclosing type.
public bool IsPublic { get; set; }
Property Value
IsRemoveMethod
Gets a value indicating whether the method is a remove method for an event.
public bool IsRemoveMethod { get; }
Property Value
IsReuseSlot
Gets or sets a value indicating whether the runtime should reuse an existing slot in the VTable of the enclosing class for this method.
public bool IsReuseSlot { get; set; }
Property Value
IsRuntime
Gets or sets a value indicating the method body is implemented by the runtime.
public bool IsRuntime { get; set; }
Property Value
IsRuntimeSpecialName
Gets or sets a value indicating the method is given a special name that is used by the runtime.
public bool IsRuntimeSpecialName { get; set; }
Property Value
IsSetMethod
Gets a value indicating whether the method is a set method for a property.
public bool IsSetMethod { get; }
Property Value
IsSpecialName
Gets or sets a value indicating the method is given a special name.
public bool IsSpecialName { get; set; }
Property Value
IsStatic
Gets or sets a value indicating whether the method requires an object instance to access it.
public bool IsStatic { get; set; }
Property Value
Remarks
This property does not reflect the value of HasThis, nor will it change the value of HasThis if this property is changed. For a valid .NET image, these values should match, however.
IsSynchronized
Gets or sets a value indicating only one thread can run the method at once.
public bool IsSynchronized { get; set; }
Property Value
IsUnmanagedExport
Gets or sets a value indicating the managed method is exported by a thunk to unmanaged code.
public bool IsUnmanagedExport { get; set; }
Property Value
IsVirtual
Gets or sets a value indicating whether the method is virtual.
public bool IsVirtual { get; set; }
Property Value
Managed
Gets or sets a value indicating whether the method body is managed by the runtime.
public bool Managed { get; set; }
Property Value
MethodBody
Gets or sets the body of the method.
public MethodBody? MethodBody { get; set; }
Property Value
Remarks
Updating this property does not automatically set the appropriate implementation attributes in the ImplAttributes.
Module
Gets the module that defines the member definition or reference.
public virtual ModuleDefinition? Module { get; }
Property Value
Remarks
For member references, this does not obtain the module definition that the member is defined in. Rather, it obtains the module definition that references this reference.
Name
Gets or sets the name of the method definition.
public Utf8String? Name { get; set; }
Property Value
Remarks
This property corresponds to the Name column in the method definition table.
NativeMethodBody
Gets or sets the unmanaged native body of the method if available.
public NativeMethodBody? NativeMethodBody { get; set; }
Property Value
Remarks
If this property is set to null
, it does not necessarily mean the method does not have a method body.
There could be a managed body assigned instead, or the current method body reader that the declaring module
uses does not support reading a certain type of native method body. See the MethodBody or
HasMethodBody properties instead.
Updating this property does not automatically set the appropriate implementation attributes in the ImplAttributes.
NoInlining
Gets or sets a value indicating whether the method can be inlined by the runtime or not.
public bool NoInlining { get; set; }
Property Value
ParameterDefinitions
Gets a collection of parameter definitions that this method defines.
public IList<ParameterDefinition> ParameterDefinitions { get; }
Property Value
Remarks
This property might not reflect the list of actual parameters that the method defines and uses according to the method signature. This property only reflects the list that is inferred from the ParamList column in the metadata row. For the actual list of parameters, use the Parameters property instead.
Parameters
Gets a collection of parameters that the method signature defines.
public ParameterCollection Parameters { get; }
Property Value
PreserveSignature
Gets or sets a value indicating whether the method's signature is not to be mangled to do HRESULT conversion.
public bool PreserveSignature { get; set; }
Property Value
RequireSecObject
Gets or sets a value indicating themethod calls another method containing security code.
public bool RequireSecObject { get; set; }
Property Value
SecurityDeclarations
Gets a collection of security declarations assigned to the member.
public IList<SecurityDeclaration> SecurityDeclarations { get; }
Property Value
Semantics
Gets the semantics associated to this method (if available).
public MethodSemantics? Semantics { get; set; }
Property Value
Signature
Gets or sets the signature of the method This includes the return type, as well as the types of the parameters that this method defines.
public MethodSignature? Signature { get; set; }
Property Value
Unmanaged
Gets or sets a value indicating whether the method body is not managed by the runtime.
public bool Unmanaged { get; set; }
Property Value
Methods
CanAccessDefinition(IMemberDefinition)
Determines whether the provided definition can be accessed by the method.
public bool CanAccessDefinition(IMemberDefinition definition)
Parameters
definition
IMemberDefinitionThe definition to access.
Returns
- bool
true
if this method can accessdefinition
,false
otherwise.
CreateConstructor(ModuleDefinition, params TypeSignature[])
Creates a new public constructor for a type that is executed when its declaring type is loaded by the CLR.
public static MethodDefinition CreateConstructor(ModuleDefinition module, params TypeSignature[] parameterTypes)
Parameters
module
ModuleDefinitionThe target module the method will be added to.
parameterTypes
TypeSignature[]An ordered list of types the parameters of the constructor should have.
Returns
- MethodDefinition
The constructor.
Remarks
The resulting method's body will consist of a single ret
instruction, and does not contain a call to
any of the declaring type's base classes. For an idiomatic .NET binary, this should be added.
CreateStaticConstructor(ModuleDefinition)
Creates a new private static constructor for a type that is executed when its declaring type is loaded by the CLR.
public static MethodDefinition CreateStaticConstructor(ModuleDefinition module)
Parameters
module
ModuleDefinitionThe target module the method will be added to.
Returns
- MethodDefinition
The constructor.
Remarks
The resulting method's body will consist of a single ret
instruction.
GetBody()
Obtains the body of the method definition.
protected virtual MethodBody? GetBody()
Returns
- MethodBody
The signature.
Remarks
This method is called upon initialization of the MethodBody property.
GetCustomAttributes()
Obtains the list of custom attributes assigned to the member.
protected virtual IList<CustomAttribute> GetCustomAttributes()
Returns
- IList<CustomAttribute>
The attributes
Remarks
This method is called upon initialization of the CustomAttributes property.
GetDeclaringType()
Obtains the declaring type of the method definition.
protected virtual TypeDefinition? GetDeclaringType()
Returns
- TypeDefinition
The declaring type.
Remarks
This method is called upon initialization of the DeclaringType property.
GetExportInfo()
Obtains the unmanaged export information associated to the method (if available).
protected virtual UnmanagedExportInfo? GetExportInfo()
Returns
- UnmanagedExportInfo?
The export information or
null
if the method was not exported as a native symbol.
Remarks
This method is called upon initialization of the ExportInfo property.
GetGenericParameters()
Obtains the list of generic parameters this member declares.
protected virtual IList<GenericParameter> GetGenericParameters()
Returns
- IList<GenericParameter>
The generic parameters
Remarks
This method is called upon initialization of the GenericParameters property.
GetImplementationMap()
Obtains the platform invoke information assigned to the method.
protected virtual ImplementationMap? GetImplementationMap()
Returns
- ImplementationMap
The mapping.
Remarks
This method is called upon initialization of the ImplementationMap property.
GetName()
Obtains the name of the method definition.
protected virtual Utf8String? GetName()
Returns
- Utf8String
The name.
Remarks
This method is called upon initialization of the Name property.
GetParameterDefinitions()
Obtains the parameter definitions of the method definition.
protected virtual IList<ParameterDefinition> GetParameterDefinitions()
Returns
- IList<ParameterDefinition>
The signature.
Remarks
This method is called upon initialization of the ParameterDefinitions property.
GetSecurityDeclarations()
Obtains the list of security declarations assigned to the member.
protected virtual IList<SecurityDeclaration> GetSecurityDeclarations()
Returns
- IList<SecurityDeclaration>
The security declarations
Remarks
This method is called upon initialization of the SecurityDeclarations property.
GetSemantics()
Obtains the semantics associated to the method (if available).
protected virtual MethodSemantics? GetSemantics()
Returns
- MethodSemantics
The semantics, or
null
if the method was not assigned semantics.
Remarks
This method is called upon initialization of the Semantics property.
GetSignature()
Obtains the signature of the method definition.
protected virtual MethodSignature? GetSignature()
Returns
- MethodSignature
The signature.
Remarks
This method is called upon initialization of the Signature property.
ImportWith(ReferenceImporter)
Imports the method using the provided reference importer object.
public IMethodDefOrRef ImportWith(ReferenceImporter importer)
Parameters
importer
ReferenceImporterThe reference importer to use.
Returns
- IMethodDefOrRef
The imported method.
IsAccessibleFromType(TypeDefinition)
Determines whether the member can be accessed from the scope that is determined by the provided type.
public bool IsAccessibleFromType(TypeDefinition type)
Parameters
type
TypeDefinitionThe type defining the scope.
Returns
- bool
True if the scope of the provided type can access the member, false otherwise.
IsImportedInModule(ModuleDefinition)
Determines whether the descriptor of the member is fully imported in the provided module.
public bool IsImportedInModule(ModuleDefinition module)
Parameters
module
ModuleDefinitionThe module that is supposed to import the member.
Returns
- bool
true
if the descriptor of the member is fully imported by the module,false
otherwise.
Remarks
This method verifies all references in the descriptor of the member only. It does not verify any additional data or contents (such as a method body) associated to the member.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.