writeAssemblyRef method
Adds an AssemblyRef
row representing the given namespace to the file,
returning its row index.
This method generates a synthetic AssemblyRef
for every two-level
namespace (e.g., Windows.Foundation
) to avoid requiring full assembly
resolution. Namespaces starting with System
are mapped to mscorlib
.
Note that when toBytes is called, the redundant AssemblyRef
rows will
be removed and the TypeRef
resolution scopes will be updated to point
to the Module
instead of the AssemblyRef
.
Implementation
AssemblyRefIndex writeAssemblyRef({required String namespace}) {
final rootNamespace = namespace.split('.').take(2).join('.');
// Use a shared reference for System.*
if (_assemblyRefs.isNotEmpty && rootNamespace.startsWith('System')) {
return _assemblyRefs['System']!;
}
// Return existing reference if present
if (_assemblyRefs[rootNamespace] case final existing?) return existing;
final table = _tableStream[MetadataTableId.assemblyRef];
final index = AssemblyRefIndex(table.length);
final assemblyRef = rootNamespace.startsWith('System')
? AssemblyRef(
name: _stringHeap.insert('mscorlib'),
majorVersion: 4,
publicKeyOrToken: _blobHeap.insert(
Uint8List.fromList([
0xB7, 0x7A, 0x5C, 0x56, 0x19, 0x34, 0xE0, 0x89, //
]),
),
)
: AssemblyRef(
name: _stringHeap.insert(rootNamespace),
majorVersion: 0xFF,
minorVersion: 0xFF,
buildNumber: 0xFF,
revisionNumber: 0xFF,
flags: AssemblyFlags.windowsRuntime,
);
table.add(assemblyRef);
_assemblyRefs[rootNamespace.startsWith('System')
? 'System'
: rootNamespace] =
index;
return index;
}