Struct MemberRefBuilder

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

Builder for creating MemberRef metadata entries.

MemberRefBuilder provides a fluent API for creating MemberRef table entries with validation and automatic heap management. Member references define external member access patterns enabling cross-assembly interoperability, late binding, dynamic member access, and generic type instantiation scenarios.

§Member Reference Model

.NET member references follow a standard pattern:

  • Declaring Context: The type, module, or method that declares the member
  • Member Identity: The name and signature that uniquely identifies the member
  • Signature Information: Type information for proper invocation and access
  • External Resolution: Runtime resolution to actual implementation

§Coded Index Types

Member references use the MemberRefParent coded index to specify the declaring context:

  • TypeDef: Members declared in current assembly types
  • TypeRef: Members declared in external assembly types
  • ModuleRef: Global members declared in external modules
  • MethodDef: Vararg method signatures referencing specific methods
  • TypeSpec: Members of generic type instantiations

§Member Types

Member references support two fundamental member types:

  • Method References: Constructor calls, method invocations, function pointers
  • Field References: Field access, property backing fields, static data

§Examples

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

// Create a method reference to external assembly
let external_type = CodedIndex::new(TableId::TypeRef, 1, CodedIndexType::MemberRefParent); // System.String from mscorlib
let method_signature = &[0x20, 0x01, 0x01, 0x0E]; // Default instance method, 1 param, void return, string param

let string_concat_ref = MemberRefBuilder::new()
    .class(external_type.clone())
    .name("Concat")
    .signature(method_signature)
    .build(&mut context)?;

// Create a field reference to external type
let field_signature = &[0x06, 0x08]; // Field signature, int32 type
let field_ref = MemberRefBuilder::new()
    .class(external_type.clone())
    .name("Length")
    .signature(field_signature)
    .build(&mut context)?;

// Create a constructor reference
let ctor_signature = &[0x20, 0x01, 0x01, 0x1C]; // Default instance method, 1 param, void return, object param
let ctor_ref = MemberRefBuilder::new()
    .class(external_type)
    .name(".ctor")
    .signature(ctor_signature)
    .build(&mut context)?;

Implementations§

§

impl MemberRefBuilder

pub fn new() -> Self

Creates a new MemberRefBuilder.

§Returns

A new crate::metadata::tables::memberref::MemberRefBuilder instance ready for configuration.

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

Sets the declaring class, module, or method for this member reference.

The class must be a valid MemberRefParent coded index that references the context where this member is declared. This establishes the scope for member resolution and access validation.

Valid class types include:

  • TypeDef - Members declared in current assembly types
  • TypeRef - Members declared in external assembly types
  • ModuleRef - Global members declared in external modules
  • MethodDef - Vararg method signatures referencing specific methods
  • TypeSpec - Members of generic type instantiations
§Arguments
  • class - A MemberRefParent coded index pointing to the declaring context
§Returns

Self for method chaining.

pub fn name(self, name: impl Into<String>) -> Self

Sets the member name for identification and access.

Member names are used for resolution, binding, and reflection operations. Common naming patterns include:

  • Standard method names: “ToString”, “GetHashCode”, “Equals”
  • Constructor names: “.ctor” (instance), “.cctor” (static)
  • Field names: “value__” (enum backing), descriptive identifiers
  • Property accessor names: “get_PropertyName”, “set_PropertyName”
§Arguments
  • name - The member name (must be a valid identifier)
§Returns

Self for method chaining.

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

Sets the member signature for type information and calling conventions.

The signature defines the member’s type structure using ECMA-335 signature encoding. The signature format depends on the member type being referenced.

Method signature patterns:

  • [0x20, 0x00, 0x01] - Default instance method, no params, void return
  • [0x00, 0x01, 0x08, 0x08] - Static method, 1 param, int32 return, int32 param
  • [0x20, 0x02, 0x0E, 0x08, 0x1C] - Instance method, 2 params, string return, int32+object params

Field signature patterns:

  • [0x06, 0x08] - Field signature, int32 type
  • [0x06, 0x0E] - Field signature, string type
  • [0x06, 0x1C] - Field signature, object type
§Arguments
  • signature - The member signature bytes
§Returns

Self for method chaining.

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

Builds the member reference and adds it to the assembly.

This method validates all required fields are set, adds the name and signature to the appropriate heaps, creates the raw member reference structure, and adds it to the MemberRef table.

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

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

§Errors
  • Returns error if class is not set
  • Returns error if name is not set
  • Returns error if signature is not set
  • Returns error if class is not a valid MemberRefParent coded index
  • Returns error if heap operations fail
  • Returns error if table operations fail

Trait Implementations§

§

impl Default for MemberRefBuilder

§

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.