Struct MethodSpecBuilder

pub struct MethodSpecBuilder { /* private fields */ }
Expand description

Builder for creating MethodSpec metadata entries.

MethodSpecBuilder provides a fluent API for creating MethodSpec table entries with validation and automatic blob management. Method specifications define instantiations of generic methods with concrete type arguments, enabling type-safe generic method dispatch and runtime generic method resolution.

§Generic Method Instantiation Model

.NET generic method instantiation follows a structured pattern:

  • Generic Method: The parameterized method definition or reference
  • Type Arguments: Concrete types that replace generic parameters
  • Instantiation Signature: Binary encoding of the type arguments
  • Runtime Resolution: Type-safe method dispatch with concrete types

§Coded Index Types

Method specifications use the MethodDefOrRef coded index to specify targets:

  • MethodDef: Generic methods defined within the current assembly
  • MemberRef: Generic methods from external assemblies or references

§Generic Method Scenarios and Patterns

Different instantiation patterns serve various generic programming scenarios:

  • Simple Instantiation: List<T>.Add(T)List<int>.Add(int)
  • Multiple Parameters: Dictionary<TKey, TValue>.TryGetValueDictionary<string, int>.TryGetValue
  • Nested Generics: Task<List<T>>Task<List<string>>
  • Constraint Satisfaction: Generic methods with type constraints
  • Variance Support: Covariant and contravariant generic parameters

§Method Specification Signatures

Instantiation signatures are stored as binary blobs containing:

  • Generic Argument Count: Number of type arguments provided
  • Type Signatures: Encoded signatures for each concrete type argument
  • Constraint Validation: Ensuring type arguments satisfy constraints
  • Variance Information: Covariance and contravariance specifications

§Examples

let assembly = CilAssembly::new(view);
let mut context = BuilderContext::new(assembly);

// Instantiate a generic method with a single type argument
let generic_method = CodedIndex::new(TableId::MethodDef, 1, CodedIndexType::MethodDefOrRef); // Generic Add<T> method
let int_instantiation = vec![
    0x01, // Generic argument count (1)
    0x08, // ELEMENT_TYPE_I4 (int32)
];

let add_int = MethodSpecBuilder::new()
    .method(generic_method)
    .instantiation(&int_instantiation)
    .build(&mut context)?;

// Instantiate a generic method with multiple type arguments
let dictionary_method = CodedIndex::new(TableId::MemberRef, 1, CodedIndexType::MethodDefOrRef); // Dictionary<TKey, TValue>.TryGetValue
let string_int_instantiation = vec![
    0x02, // Generic argument count (2)
    0x0E, // ELEMENT_TYPE_STRING
    0x08, // ELEMENT_TYPE_I4 (int32)
];

let trygetvalue_string_int = MethodSpecBuilder::new()
    .method(dictionary_method)
    .instantiation(&string_int_instantiation)
    .build(&mut context)?;

// Instantiate a generic method with complex type arguments
let complex_method = CodedIndex::new(TableId::MethodDef, 2, CodedIndexType::MethodDefOrRef); // Complex generic method
let complex_instantiation = vec![
    0x01, // Generic argument count (1)
    0x1D, // ELEMENT_TYPE_SZARRAY (single-dimensional array)
    0x0E, // Array element type: ELEMENT_TYPE_STRING
];

let complex_string_array = MethodSpecBuilder::new()
    .method(complex_method)
    .instantiation(&complex_instantiation)
    .build(&mut context)?;

// Instantiate with a reference to another type
let reference_method = CodedIndex::new(TableId::MemberRef, 2, CodedIndexType::MethodDefOrRef); // Generic method reference
let typeref_instantiation = vec![
    0x01, // Generic argument count (1)
    0x12, // ELEMENT_TYPE_CLASS
    0x02, // TypeDefOrRef coded index (simplified)
];

let typeref_instantiation_spec = MethodSpecBuilder::new()
    .method(reference_method)
    .instantiation(&typeref_instantiation)
    .build(&mut context)?;

Implementations§

§

impl MethodSpecBuilder

pub fn new() -> Self

Creates a new MethodSpecBuilder.

§Returns

A new crate::metadata::tables::methodspec::MethodSpecBuilder instance ready for configuration.

pub fn method(self, method: CodedIndex) -> Self

Sets the generic method that will be instantiated.

The method must be a valid MethodDefOrRef coded index that references either a generic method definition or a generic method reference. This establishes which generic method template will be instantiated with concrete type arguments.

Valid method types include:

  • MethodDef - Generic methods defined within the current assembly
  • MemberRef - Generic methods from external assemblies or references

Generic method considerations:

  • Method Definition: Must be a generic method with type parameters
  • Type Constraints: Type arguments must satisfy method constraints
  • Accessibility: Instantiation must respect method visibility
  • Assembly Boundaries: External methods require proper assembly references
§Arguments
  • method - A MethodDefOrRef coded index pointing to the generic method
§Returns

Self for method chaining.

pub fn instantiation(self, instantiation: &[u8]) -> Self

Sets the instantiation signature specifying concrete type arguments.

The instantiation signature defines the concrete types that will replace the generic parameters in the method definition. This binary signature is stored in the blob heap and follows .NET’s method specification format.

Signature structure:

  • Generic Argument Count: Number of type arguments (compressed integer)
  • Type Arguments: Type signatures for each concrete type argument
  • Type Encoding: Following ELEMENT_TYPE constants and encoding rules
  • Reference Resolution: TypeDefOrRef coded indexes for complex types

Common signature patterns:

  • Primitive Types: Single byte ELEMENT_TYPE values (I4, STRING, etc.)
  • Reference Types: ELEMENT_TYPE_CLASS followed by TypeDefOrRef coded index
  • Value Types: ELEMENT_TYPE_VALUETYPE followed by TypeDefOrRef coded index
  • Arrays: ELEMENT_TYPE_SZARRAY followed by element type signature
  • Generic Types: ELEMENT_TYPE_GENERICINST with type definition and arguments
§Arguments
  • instantiation - The binary signature containing concrete type arguments
§Returns

Self for method chaining.

pub fn simple_instantiation(self, element_type: u8) -> Self

Sets a simple single-type instantiation for common scenarios.

This convenience method creates an instantiation signature for generic methods with a single type parameter, using a primitive type specified by its ELEMENT_TYPE constant.

§Arguments
  • element_type - The ELEMENT_TYPE constant for the concrete type argument
§Returns

Self for method chaining.

pub fn multiple_primitives(self, element_types: &[u8]) -> Self

Sets an instantiation with multiple primitive type arguments.

This convenience method creates an instantiation signature for generic methods with multiple type parameters, all using primitive types.

§Arguments
  • element_types - Array of ELEMENT_TYPE constants for each type argument
§Returns

Self for method chaining.

pub fn array_instantiation(self, element_type: u8) -> Self

Sets an instantiation with a single array type argument.

This convenience method creates an instantiation signature for generic methods instantiated with a single-dimensional array type.

§Arguments
  • element_type - The ELEMENT_TYPE constant for the array element type
§Returns

Self for method chaining.

pub fn build(self, context: &mut BuilderContext) -> Result<Token>

Builds the method specification entry and adds it to the assembly.

This method validates all required fields are set, adds the instantiation signature to the blob heap, creates the raw method specification structure, and adds it to the MethodSpec table with proper token generation.

§Arguments
  • context - The builder context for managing the assembly
§Returns

A crate::metadata::token::Token representing the newly created method specification, or an error if validation fails or required fields are missing.

§Errors
  • Returns error if method is not set
  • Returns error if instantiation is not set or empty
  • Returns error if method is not a valid MethodDefOrRef coded index
  • Returns error if blob operations fail
  • Returns error if table operations fail

Trait Implementations§

§

impl Default for MethodSpecBuilder

§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.