### Unmanaged Assembly Loading and Resolution Setup Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/TempFileTests.TemplateHasCorrectSymbols.DotNet8_0.Debug.verified.txt This IL code snippet demonstrates the logic for loading unmanaged assemblies into a temporary path and setting up the assembly resolution event handler. It includes steps for getting the temporary path, combining it with a checksum identifier, preloading unmanaged libraries, and attaching a custom assembly resolver to the default AssemblyLoadContext. ```IL IL_000f: stloc.s V_4 IL_0011: ldloc.s V_4 IL_0013: brfalse.s IL_001b IL_0015: nop IL_0016: br IL_009e IL_001b: ldstr "[CHECKSUM]" IL_0020: stloc.0 IL_0021: call string [System.Private.CoreLib]System.IO.Path::GetTempPath() IL_0026: ldstr "Costura" IL_002b: call string [System.Private.CoreLib]System.IO.Path::Combine(string, string) IL_0030: stloc.1 IL_0031: ldloc.1 IL_0032: ldloc.0 IL_0033: call string [System.Private.CoreLib]System.IO.Path::Combine(string, string) IL_0038: stsfld string Costura.AssemblyLoader::tempBasePath IL_003d: call class [System.Private.CoreLib]System.Collections.Generic.List`1 Costura.AssemblyLoader::GetUnmanagedAssemblies() IL_0042: stloc.2 IL_0043: newobj instance void class [System.Private.CoreLib]System.Collections.Generic.List`1::.ctor() IL_0048: stloc.3 IL_0049: ldloc.3 IL_004a: ldloc.2 IL_004b: callvirt instance void class [System.Private.CoreLib]System.Collections.Generic.List`1::AddRange(class [System.Private.CoreLib]System.Collections.Generic.IEnumerable`1) IL_0050: nop IL_0051: ldloc.3 IL_0052: ldsfld class [System.Private.CoreLib]System.Collections.Generic.List`1 Costura.AssemblyLoader::preloadList IL_0057: callvirt instance void class [System.Private.CoreLib]System.Collections.Generic.List`1::AddRange(class [System.Private.CoreLib]System.Collections.Generic.IEnumerable`1) IL_005c: nop IL_005d: ldloc.0 IL_005e: ldsfld string Costura.AssemblyLoader::tempBasePath IL_0063: ldloc.3 IL_0064: ldsfld class [System.Private.CoreLib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::checksums IL_0069: call void Costura.AssemblyLoader::PreloadUnmanagedLibraries(string, string, class [System.Private.CoreLib]System.Collections.Generic.List`1, class [System.Private.CoreLib]System.Collections.Generic.Dictionary`2) IL_006e: nop IL_006f: ldarg.0 IL_0070: stloc.s V_5 IL_0072: ldloc.s V_5 IL_0074: brfalse.s IL_009e IL_0076: nop IL_0077: call class [System.Private.CoreLib]System.Runtime.Loader.AssemblyLoadContext [System.Private.CoreLib]System.Runtime.Loader.AssemblyLoadContext::get_Default() IL_007c: ldsfld class [System.Private.CoreLib]System.Func`3 Costura.AssemblyLoader/'<>O'::'<0>__ResolveAssembly' IL_0081: dup IL_0082: brtrue.s IL_0097 IL_0084: pop IL_0085: ldnull IL_0086: ldftn class [System.Private.CoreLib]System.Reflection.Assembly Costura.AssemblyLoader::ResolveAssembly(class [System.Private.CoreLib]System.Runtime.Loader.AssemblyLoadContext, class [System.Private.CoreLib]System.Reflection.AssemblyName) IL_008c: newobj instance void class [System.Private.CoreLib]System.Func`3::.ctor(object, native int) IL_0091: dup IL_0092: stsfld class [System.Private.CoreLib]System.Func`3 Costura.AssemblyLoader/'<>O'::'<0>__ResolveAssembly' IL_0097: callvirt instance void [System.Private.CoreLib]System.Runtime.Loader.AssemblyLoadContext::add_Resolving(class [System.Private.CoreLib]System.Func`3) IL_009c: nop IL_009d: nop IL_009e: ret ``` -------------------------------- ### Get Unmanaged Assemblies Based on OS Architecture (IL) Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/TempFileTests.TemplateHasCorrectSymbols.DotNet8_0.Debug.verified.txt This IL method determines the operating system architecture (x86, x64, Arm64) and returns a corresponding list of unmanaged assemblies to be preloaded. It throws a NotSupportedException for unsupported architectures or platforms. ```il .method private hidebysig static class [System.Private.CoreLib]System.Collections.Generic.List`1 GetUnmanagedAssemblies() cil managed { .maxstack 2 .locals init (valuetype [System.Private.CoreLib]System.Runtime.InteropServices.Architecture V_0, bool V_1, valuetype [System.Private.CoreLib]System.Runtime.InteropServices.Architecture V_2, valuetype [System.Private.CoreLib]System.Runtime.InteropServices.Architecture V_3, class [System.Private.CoreLib]System.Collections.Generic.List`1 V_4) IL_0000: nop IL_0001: call valuetype [System.Private.CoreLib]System.Runtime.InteropServices.Architecture [System.Private.CoreLib]System.Runtime.InteropServices.RuntimeInformation::get_ProcessArchitecture() IL_0006: stloc.0 IL_0007: call valuetype [System.Private.CoreLib]System.Runtime.InteropServices.OSPlatform [System.Private.CoreLib]System.Runtime.InteropServices.OSPlatform::get_Windows() IL_000c: call bool [System.Private.CoreLib]System.Runtime.InteropServices.RuntimeInformation::IsOSPlatform(valuetype [System.Private.CoreLib]System.Runtime.InteropServices.OSPlatform) IL_0011: stloc.1 IL_0012: ldloc.1 IL_0013: brfalse.s IL_0063 IL_0015: nop IL_0016: ldloc.0 IL_0017: stloc.3 IL_0018: ldloc.0 IL_0019: stloc.2 IL_001a: ldloc.2 IL_001b: switch ( IL_003b, IL_0044, IL_004d, IL_0032) IL_0030: br.s IL_004d IL_0032: ldsfld class [System.Private.CoreLib]System.Collections.Generic.List`1 Costura.AssemblyLoader::preloadWinArm64List IL_0037: stloc.s V_4 IL_0039: br.s IL_006e IL_003b: ldsfld class [System.Private.CoreLib]System.Collections.Generic.List`1 Costura.AssemblyLoader::preloadWinX86List IL_0040: stloc.s V_4 IL_0042: br.s IL_006e IL_0044: ldsfld class [System.Private.CoreLib]System.Collections.Generic.List`1 Costura.AssemblyLoader::preloadWinX64List IL_0049: stloc.s V_4 IL_004b: br.s IL_006e IL_004d: ldstr "Architecture '{0}' not supported" IL_0052: ldloc.0 IL_0053: box [System.Private.CoreLib]System.Runtime.InteropServices.Architecture IL_0058: call string [System.Private.CoreLib]System.String::Format(string, object) IL_005d: newobj instance void [System.Private.CoreLib]System.NotSupportedException::.ctor(string) IL_0062: throw IL_0063: ldstr "Platform is not (yet) supported" IL_0068: newobj instance void [System.Private.CoreLib]System.NotSupportedException::.ctor(string) IL_006d: throw IL_006e: ldloc.s V_4 IL_0070: ret } ``` -------------------------------- ### CIL Exception Handling and Directory Creation Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/MixedAndNativeTests.TemplateHasCorrectSymbols.DotNet8_0.Debug.verified.txt Contains CIL instructions for handling unsupported architectures and a method for creating directories if they do not exist. ```CIL IL_004b: br.s IL_006e IL_004d: ldstr "Architecture '{0}' not supported" IL_0052: ldloc.0 IL_0053: box [System.Private.CoreLib]System.Runtime.InteropServices.Architecture IL_0058: call string [System.Private.CoreLib]System.String::Format(string, object) IL_005d: newobj instance void [System.Private.CoreLib]System.NotSupportedException::.ctor(string) IL_0062: throw IL_0063: ldstr "Platform is not (yet) supported" IL_0068: newobj instance void [System.Private.CoreLib]System.NotSupportedException::.ctor(string) IL_006d: throw IL_006e: ldloc.s V_4 IL_0070: ret } .method private hidebysig static void CreateDirectory(string tempBasePath) cil managed { .maxstack 2 .locals init (bool V_0) IL_0000: nop IL_0001: ldarg.0 IL_0002: call bool [System.Private.CoreLib]System.IO.Directory::Exists(string) IL_0007: ldc.i4.0 IL_0008: ceq IL_000a: stloc.0 IL_000b: ldloc.0 IL_000c: brfalse.s IL_0017 IL_000e: nop IL_000f: ldarg.0 IL_0010: call class [System.Private.CoreLib]System.IO.DirectoryInfo [System.Private.CoreLib]System.IO.Directory::CreateDirectory(string) IL_0015: pop IL_0016: nop IL_0017: ret } ``` -------------------------------- ### Initialize Assembly and Symbol Dictionaries Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/ReferenceCasingTests.TemplateHasCorrectSymbols.Net4_7.Debug.verified.txt IL instructions for populating the internal dictionaries used to map assembly and symbol names to their compressed resource identifiers. ```IL IL_001e: newobj instance void class [mscorlib]System.Collections.Generic.Dictionary`2::.ctor() IL_0023: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::symbolNames IL_0028: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::assemblyNames IL_002d: ldstr "assemblytoreference" IL_0032: ldstr "costura.assemblytoreference.dll.compressed" IL_0037: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_003c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::symbolNames IL_0041: ldstr "assemblytoreference" IL_0046: ldstr "costura.assemblytoreference.pdb.compressed" IL_004b: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_0050: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::assemblyNames IL_0055: ldstr "exetoreference" IL_005a: ldstr "costura.exetoreference.exe.compressed" IL_005f: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_0064: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::symbolNames IL_0069: ldstr "exetoreference" IL_006e: ldstr "costura.exetoreference.pdb.compressed" IL_0073: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_0078: ret ``` -------------------------------- ### IL Code for Getting Executing Assembly Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/CultureResourceTests.TemplateHasCorrectSymbols.DotNet8_0.Debug.verified.txt This IL code snippet shows how to get the currently executing assembly. ```IL .method private hidebysig static class [System.Private.CoreLib]System.Reflection.Assembly ``` -------------------------------- ### String Formatting and Exception Handling Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/MixedAndNativeTests.TemplateHasCorrectSymbols.DotNet8_0.Debug.verified.txt Demonstrates string formatting for assembly names and platform checks, including throwing a NotSupportedException for unsupported architectures. ```CIL IL_0067: call string [System.Private.CoreLib]System.String::Format(string, object, object) IL_006c: stloc.s V_5 IL_006e: br.s IL_009a IL_0070: ldstr "{0}-{1}" IL_0075: ldloc.0 IL_0076: ldstr "x64" IL_007b: call string [System.Private.CoreLib]System.String::Format(string, object, object) IL_0080: stloc.s V_5 IL_0082: br.s IL_009a IL_0084: ldstr "Architecture '{0}' not supported" IL_0089: ldloc.1 IL_008a: box [System.Private.CoreLib]System.Runtime.InteropServices.Architecture IL_008f: call string [System.Private.CoreLib]System.String::Format(string, object) IL_0094: newobj instance void [System.Private.CoreLib]System.NotSupportedException::.ctor(string) IL_0099: throw IL_009a: ldloc.s V_5 IL_009c: ret } ``` -------------------------------- ### Install Fody and Costura.Fody NuGet Packages Source: https://github.com/fody/costura/blob/develop/readme.md Install the necessary NuGet packages for Fody and Costura.Fody using the Package Manager Console. ```powershell PM> Install-Package Fody PM> Install-Package Costura.Fody ``` -------------------------------- ### Assembly Resolve Event Handler Setup Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/CultureResourceTests.TemplateHasCorrectSymbols.Net4_7.Debug.verified.txt This IL code demonstrates the setup of an AssemblyResolve event handler for System.AppDomain. It's crucial for Costura's mechanism of embedding and resolving assemblies at runtime. ```IL IL_002d: ldftn class [mscorlib]System.Reflection.Assembly Costura.AssemblyLoader::ResolveAssembly(object, class [mscorlib]System.ResolveEventArgs) IL_0033: newobj instance void [mscorlib]System.ResolveEventHandler::.ctor(object, native int) IL_0038: dup IL_0039: stsfld class [mscorlib]System.ResolveEventHandler Costura.AssemblyLoader/'<>O'::'<0>__ResolveAssembly' IL_003e: callvirt instance void [mscorlib]System.AppDomain::add_AssemblyResolve(class [mscorlib]System.ResolveEventHandler) IL_0043: nop IL_0044: nop IL_0045: ret } ``` -------------------------------- ### InternalPreloadUnmanagedLibraries Implementation Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/MixedAndNativeTests.TemplateHasCorrectSymbols.DotNet8_0.Debug.verified.txt Core logic for iterating through libraries, resolving paths, and verifying file checksums before preloading. ```IL .method private hidebysig static void InternalPreloadUnmanagedLibraries(string tempBasePath, class [System.Private.CoreLib]System.Collections.Generic.IList`1 libs, class [System.Private.CoreLib]System.Collections.Generic.Dictionary`2 checksums) cil managed { .maxstack 5 .locals init (string V_0, uint32 V_1, uint32 V_2, class [System.Private.CoreLib]System.Collections.Generic.IEnumerator`1 V_3, string V_4, string V_5, bool V_6, string V_7, bool V_8, bool V_9, class [System.Private.CoreLib]System.IO.Stream V_10, class [System.Private.CoreLib]System.IO.FileStream V_11, class [System.Private.CoreLib]System.Collections.Generic.IEnumerator`1 V_12, string V_13, bool V_14, string V_15) IL_0000: nop IL_0001: nop IL_0002: ldarg.1 IL_0003: callvirt instance class [System.Private.CoreLib]System.Collections.Generic.IEnumerator`1 class [System.Private.CoreLib]System.Collections.Generic.IEnumerable`1::GetEnumerator() IL_0008: stloc.3 .try { IL_0009: br IL_00c8 IL_000e: ldloc.3 IL_000f: callvirt instance !0 class [System.Private.CoreLib]System.Collections.Generic.IEnumerator`1::get_Current() IL_0014: stloc.s V_4 IL_0016: nop IL_0017: ldloc.s V_4 IL_0019: call string Costura.AssemblyLoader::ResourceNameToPath(string) IL_001e: stloc.0 IL_001f: ldarg.0 IL_0020: ldloc.0 IL_0021: call string [System.Private.CoreLib]System.IO.Path::Combine(string, string) IL_0026: stloc.s V_5 IL_0028: ldstr "Preloading unmanaged library '{0}' to '{1}'" IL_002d: ldc.i4.2 IL_002e: newarr [System.Private.CoreLib]System.Object IL_0033: dup IL_0034: ldc.i4.0 IL_0035: ldloc.0 IL_0036: stelem.ref IL_0037: dup IL_0038: ldc.i4.1 IL_0039: ldloc.s V_5 IL_003b: stelem.ref IL_003c: call void Costura.AssemblyLoader::Log(string, object[]) IL_0041: nop IL_0042: ldloc.s V_5 IL_0044: call bool [System.Private.CoreLib]System.IO.File::Exists(string) IL_0049: stloc.s V_6 IL_004b: ldloc.s V_6 IL_004d: brfalse.s IL_0079 IL_004f: nop IL_0050: ldloc.s V_5 IL_0052: call string Costura.AssemblyLoader::CalculateChecksum(string) IL_0057: stloc.s V_7 IL_0059: ldloc.s V_7 IL_005b: ldarg.2 IL_005c: ldloc.s V_4 IL_005e: callvirt instance !1 class [System.Private.CoreLib]System.Collections.Generic.Dictionary`2::get_Item(!0) IL_0063: call bool [System.Private.CoreLib]System.String::op_Inequality(string, string) IL_0068: stloc.s V_8 IL_006a: ldloc.s V_8 IL_006c: brfalse.s IL_0078 IL_006e: nop IL_006f: ldloc.s V_5 ``` -------------------------------- ### ResolveAssembly CIL Method Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/InitializeCallTests.TemplateHasCorrectSymbols.Net4_7.Debug.verified.txt Entry point for assembly resolution events. This method handles the initial setup for resolving missing assemblies. ```CIL .method public hidebysig static class [mscorlib]System.Reflection.Assembly ResolveAssembly(object sender, class [mscorlib]System.ResolveEventArgs e) cil managed { .maxstack 5 .locals init (string V_0, class [mscorlib]System.Reflection.AssemblyName V_1, class [mscorlib]System.Reflection.Assembly V_2, object V_3, bool V_4, bool V_5, class [mscorlib]System.Reflection.Assembly V_6, bool V_7, bool V_8, object V_9, bool V_10, bool V_11) IL_0000: nop ``` -------------------------------- ### Assembly Loading Logic (IL) Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/TempFileTests.TemplateHasCorrectSymbols.DotNet8_0.Release.verified.txt This IL code snippet demonstrates the logic for loading an assembly from disk. It checks for the existence of a DLL, and if not found, attempts to change the extension to EXE and check again. If still not found, it constructs a new path and checks for a DLL and then an EXE. ```IL IL_000c: stloc.1 ldarg.0 ldloc.0 ldstr ".dll" call string [System.Private.CoreLib]System.String::Concat(string, string) call string [System.Private.CoreLib]System.IO.Path::Combine(string, string) stloc.2 ldloc.2 call bool [System.Private.CoreLib]System.IO.File::Exists(string) brfalse.s IL_002e ldloc.2 call class [System.Private.CoreLib]System.Reflection.Assembly [System.Private.CoreLib]System.Reflection.Assembly::LoadFile(string) ret IL_002e: ldloc.2 ldstr "exe" call string [System.Private.CoreLib]System.IO.Path::ChangeExtension(string, string) stloc.2 ldloc.2 call bool [System.Private.CoreLib]System.IO.File::Exists(string) brfalse.s IL_0049 ldloc.2 call class [System.Private.CoreLib]System.Reflection.Assembly [System.Private.CoreLib]System.Reflection.Assembly::LoadFile(string) ret IL_0049: ldarg.0 ldloc.1 call string [System.Private.CoreLib]System.IO.Path::Combine(string, string) ldloc.0 ldstr ".dll" call string [System.Private.CoreLib]System.String::Concat(string, string) call string [System.Private.CoreLib]System.IO.Path::Combine(string, string) stloc.2 ldloc.2 call bool [System.Private.CoreLib]System.IO.File::Exists(string) brfalse.s IL_0070 ldloc.2 call class [System.Private.CoreLib]System.Reflection.Assembly [System.Private.CoreLib]System.Reflection.Assembly::LoadFile(string) ret IL_0070: ldloc.2 ldstr "exe" call string [System.Private.CoreLib]System.IO.Path::ChangeExtension(string, string) stloc.2 ldloc.2 call bool [System.Private.CoreLib]System.IO.File::Exists(string) brfalse.s IL_008b ldloc.2 call class [System.Private.CoreLib]System.Reflection.Assembly [System.Private.CoreLib]System.Reflection.Assembly::LoadFile(string) ret IL_008b: ldnull ret ``` -------------------------------- ### Include Runtimes by Attribute Source: https://github.com/fody/costura/blob/develop/readme.md Alternatively, include runtimes using the `IncludeRuntimes` attribute with pipe-delimited values. Example values include 'win-x64|win-arm64'. ```xml ``` -------------------------------- ### Get Platform Name (CIL) Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/MixedAndNativeTests.TemplateHasCorrectSymbols.Net4_7.Debug.verified.txt Determines the platform name (x86 or x64) based on the IntPtr size. Used for constructing platform-specific resource paths. ```cil .method private hidebysig static string GetPlatformName() cil managed { .maxstack 2 .locals init (string V_0, string V_1) IL_0000: nop IL_0001: call int32 [mscorlib]System.IntPtr::get_Size() IL_0006: ldc.i4.8 IL_0007: beq.s IL_0010 IL_0009: ldstr "86" IL_000e: br.s IL_0015 IL_0010: ldstr "64" IL_0015: stloc.0 IL_0016: ldstr "win-x" IL_001b: ldloc.0 IL_001c: call string [mscorlib]System.String::Concat(string, string) IL_0021: stloc.1 IL_0022: br.s IL_0024 IL_0024: ldloc.1 IL_0025: ret } ``` -------------------------------- ### Initialize Assembly Dictionaries in IL Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/InitializeCallTests.TemplateHasCorrectSymbols.Net4_7.Debug.verified.txt This IL snippet initializes the assemblyNames and symbolNames dictionaries with compressed resource paths. ```IL IL_001e: newobj instance void class [mscorlib]System.Collections.Generic.Dictionary`2::.ctor() IL_0023: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::symbolNames IL_0028: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::assemblyNames IL_002d: ldstr "assemblytoreference" IL_0032: ldstr "costura.assemblytoreference.dll.compressed" IL_0037: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_003c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::symbolNames IL_0041: ldstr "assemblytoreference" IL_0046: ldstr "costura.assemblytoreference.pdb.compressed" IL_004b: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_0050: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::assemblyNames IL_0055: ldstr "assemblytoreferencepreembedded" IL_005a: ldstr "costura.assemblytoreferencepreembedded.dll.compres" + "sed" IL_005f: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_0064: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::symbolNames IL_0069: ldstr "assemblytoreferencepreembedded" IL_006e: ldstr "costura.assemblytoreferencepreembedded.pdb.compres" + "sed" IL_0073: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_0078: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::assemblyNames IL_007d: ldstr "exetoreference" IL_0082: ldstr "costura.exetoreference.exe.compressed" IL_0087: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_008c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 Costura.AssemblyLoader::symbolNames IL_0091: ldstr "exetoreference" IL_0096: ldstr "costura.exetoreference.pdb.compressed" IL_009b: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_00a0: ret ``` -------------------------------- ### Get Assembly Resource Name (CIL) Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/MixedAndNativeTests.TemplateHasCorrectSymbols.Net4_7.Debug.verified.txt Generates the resource name for an assembly based on itsAssemblyName. Handles culture information to construct the full name. ```cil .method private hidebysig static string GetAssemblyResourceName(class [mscorlib]System.Reflection.AssemblyName requestedAssemblyName) cil managed { .maxstack 3 .locals init (string V_0, bool V_1, string V_2) IL_0000: nop IL_0001: ldarg.0 IL_0002: callvirt instance string [mscorlib]System.Reflection.AssemblyName::get_Name() IL_0007: callvirt instance string [mscorlib]System.String::ToLowerInvariant() IL_000c: stloc.0 IL_000d: ldarg.0 IL_000e: callvirt instance class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Reflection.AssemblyName::get_CultureInfo() IL_0013: brfalse.s IL_002a IL_0015: ldarg.0 IL_0016: callvirt instance class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Reflection.AssemblyName::get_CultureInfo() IL_001b: callvirt instance string [mscorlib]System.Globalization.CultureInfo::get_Name() IL_0020: call bool [mscorlib]System.String::IsNullOrEmpty(string) IL_0025: ldc.i4.0 IL_0026: ceq IL_0028: br.s IL_002b IL_002a: ldc.i4.0 IL_002b: stloc.1 IL_002c: ldloc.1 IL_002d: brfalse.s IL_004d IL_002f: nop IL_0030: ldarg.0 IL_0031: callvirt instance class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Reflection.AssemblyName::get_CultureInfo() IL_0036: call string Costura.AssemblyLoader::CultureToString(class [mscorlib]System.Globalization.CultureInfo) IL_003b: ldstr "." IL_0040: ldloc.0 IL_0041: call string [mscorlib]System.String::Concat(string, string, string) IL_0046: callvirt instance string [mscorlib]System.String::ToLowerInvariant() IL_004b: stloc.0 IL_004c: nop IL_004d: ldloc.0 IL_004e: stloc.2 IL_004f: br.s IL_0051 IL_0051: ldloc.2 IL_0052: ret } ``` -------------------------------- ### CreateDirectory IL Method Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/TempFileTests.TemplateHasCorrectSymbols.Net4_7.Release.verified.txt Checks if a directory exists and creates it if it does not. ```IL .method private hidebysig static void CreateDirectory(string tempBasePath) cil managed { .maxstack 8 IL_0000: ldarg.0 IL_0001: call bool [mscorlib]System.IO.Directory::Exists(string) IL_0006: brtrue.s IL_000f IL_0008: ldarg.0 IL_0009: call class [mscorlib]System.IO.DirectoryInfo [mscorlib]System.IO.Directory::CreateDirectory(string) IL_000e: pop IL_000f: ret } ``` -------------------------------- ### Handle Exception and Preload Unmanaged Libraries (IL) Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/MixedAndNativeTests.TemplateHasCorrectSymbols.DotNet8_0.Debug.verified.txt This IL code demonstrates exception handling for mutex operations and the preloading of unmanaged libraries. It constructs a temporary path, logs the operation, creates the directory, and then calls the internal preload method. ```IL IL_0028: ldc.i4.0 IL_0029: ceq IL_002b: stloc.s V_5 IL_002d: ldloc.s V_5 IL_002f: brfalse.s IL_003d IL_0031: nop IL_0032: ldstr "Timeout waiting for exclusive access" IL_0037: newobj instance void [System.Private.CoreLib]System.TimeoutException::.ctor(string) IL_003c: throw IL_003d: nop IL_003e: leave.s IL_0047 } catch [System.Private.CoreLib]System.Threading.AbandonedMutexException { IL_0040: pop IL_0041: nop IL_0042: ldc.i4.1 IL_0043: stloc.2 IL_0044: nop IL_0045: leave.s IL_0047 } IL_0047: call string Costura.AssemblyLoader::GetPlatformName() IL_004c: stloc.3 IL_004d: ldarg.1 IL_004e: ldloc.3 IL_004f: call string [System.Private.CoreLib]System.IO.Path::Combine(string, string) IL_0054: stloc.s V_4 IL_0056: ldstr "Preloading unmanaged libraries to '{0}'" IL_005b: ldc.i4.1 IL_005c: newarr [System.Private.CoreLib]System.Object IL_0061: dup IL_0062: ldc.i4.0 IL_0063: ldloc.s V_4 IL_0065: stelem.ref IL_0066: call void Costura.AssemblyLoader::Log(string, object[]) IL_006b: nop IL_006c: ldloc.s V_4 IL_006e: call void Costura.AssemblyLoader::CreateDirectory(string) IL_0073: nop IL_0074: ldarg.1 IL_0075: ldarg.2 IL_0076: ldarg.3 IL_0077: call void Costura.AssemblyLoader::InternalPreloadUnmanagedLibraries(string, class [System.Private.CoreLib]System.Collections.Generic.IList`1, class [System.Private.CoreLib]System.Collections.Generic.Dictionary`2) IL_007c: nop IL_007d: nop IL_007e: leave.s IL_0093 } finally { IL_0080: nop IL_0081: ldloc.2 IL_0082: stloc.s V_6 IL_0084: ldloc.s V_6 IL_0086: brfalse.s IL_0091 IL_0088: nop IL_0089: ldloc.1 IL_008a: callvirt instance void [System.Private.CoreLib]System.Threading.Mutex::ReleaseMutex() IL_008f: nop IL_0090: nop IL_0091: nop IL_0092: endfinally } IL_0093: nop IL_0094: leave.s IL_00a1 } finally { IL_0096: ldloc.1 IL_0097: brfalse.s IL_00a0 IL_0099: ldloc.1 IL_009a: callvirt instance void [System.Private.CoreLib]System.IDisposable::Dispose() IL_009f: nop IL_00a0: endfinally } IL_00a1: ret } ``` -------------------------------- ### Get Assembly Resource Name Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/CultureResourceTests.TemplateHasCorrectSymbols.Net4_7.Release.verified.txt Generates the resource name for an assembly based on its AssemblyName. Handles culture information to construct the full resource name. ```IL .method private hidebysig static string GetAssemblyResourceName(class [mscorlib]System.Reflection.AssemblyName requestedAssemblyName) cil managed { .maxstack 3 .locals init (string V_0) IL_0000: ldarg.0 IL_0001: callvirt instance string [mscorlib]System.Reflection.AssemblyName::get_Name() IL_0006: callvirt instance string [mscorlib]System.String::ToLowerInvariant() IL_000b: stloc.0 IL_000c: ldarg.0 IL_000d: callvirt instance class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Reflection.AssemblyName::get_CultureInfo() IL_0012: brfalse.s IL_0042 IL_0014: ldarg.0 IL_0015: callvirt instance class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Reflection.AssemblyName::get_CultureInfo() IL_001a: callvirt instance string [mscorlib]System.Globalization.CultureInfo::get_Name() IL_001f: call bool [mscorlib]System.String::IsNullOrEmpty(string) IL_0024: brtrue.s IL_0042 IL_0026: ldarg.0 IL_0027: callvirt instance class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Reflection.AssemblyName::get_CultureInfo() IL_002c: call string Costura.AssemblyLoader::CultureToString(class [mscorlib]System.Globalization.CultureInfo) IL_0031: ldstr "." IL_0036: ldloc.0 IL_0037: call string [mscorlib]System.String::Concat(string, string, string) IL_003c: callvirt instance string [mscorlib]System.String::ToLowerInvariant() IL_0041: stloc.0 IL_0042: ldloc.0 IL_0043: ret } ``` -------------------------------- ### Get Unmanaged Assemblies Based on Platform Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/TempFileTests.TemplateHasCorrectSymbols.DotNet8_0.Release.verified.txt This IL method determines the operating system and processor architecture to return the appropriate list of unmanaged assemblies for loading. ```IL .method private hidebysig static class [System.Private.CoreLib]System.Collections.Generic.List`1 GetUnmanagedAssemblies() cil managed { .maxstack 2 .locals init (valuetype [System.Private.CoreLib]System.Runtime.InteropServices.Architecture V_0) IL_0000: call valuetype [System.Private.CoreLib]System.Runtime.InteropServices.Architecture [System.Private.CoreLib]System.Runtime.InteropServices.RuntimeInformation::get_ProcessArchitecture() IL_0005: stloc.0 IL_0006: call valuetype [System.Private.CoreLib]System.Runtime.InteropServices.OSPlatform [System.Private.CoreLib]System.Runtime.InteropServices.OSPlatform::get_Windows() IL_000b: call bool [System.Private.CoreLib]System.Runtime.InteropServices.RuntimeInformation::IsOSPlatform(valuetype [System.Private.CoreLib]System.Runtime.InteropServices.OSPlatform) IL_0010: brfalse.s IL_0052 IL_0012: ldloc.0 IL_0013: switch ( IL_0030, IL_0036, IL_003c, IL_002a) IL_0028: br.s IL_003c IL_002a: ldsfld class [System.Private.CoreLib]System.Collections.Generic.List`1 Costura.AssemblyLoader::preloadWinArm64List IL_002f: ret IL_0030: ldsfld class [System.Private.CoreLib]System.Collections.Generic.List`1 Costura.AssemblyLoader::preloadWinX86List IL_0035: ret IL_0036: ldsfld class [System.Private.CoreLib]System.Collections.Generic.List`1 Costura.AssemblyLoader::preloadWinX64List IL_003b: ret IL_003c: ldstr "Architecture '{0}' not supported" IL_0041: ldloc.0 IL_0042: box [System.Private.CoreLib]System.Runtime.InteropServices.Architecture IL_0047: call string [System.Private.CoreLib]System.String::Format(string, object) IL_004c: newobj instance void [System.Private.CoreLib]System.NotSupportedException::.ctor(string) IL_0051: throw IL_0052: ldstr "Platform is not (yet) supported" IL_0057: newobj instance void [System.Private.CoreLib]System.NotSupportedException::.ctor(string) IL_005c: throw } ``` -------------------------------- ### AssemblyLoader Initialization Source: https://github.com/fody/costura/blob/develop/src/Costura.Fody.Tests/Helpers/InMemoryTests.TemplateHasCorrectSymbols.Net4_7.Debug.verified.txt This section details the initialization process of the AssemblyLoader, specifically how it populates dictionaries for assembly and symbol names. ```APIDOC ## AssemblyLoader Initialization ### Description Initializes the `symbolNames` and `assemblyNames` dictionaries with compressed and uncompressed assembly and PDB file names. ### Method Implicit constructor or static initialization block. ### Endpoint N/A (Internal class initialization) ### Request Body N/A ### Response N/A ### Internal Mappings - `assemblytoreference`: `costura.assemblytoreference.dll.compressed` - `assemblytoreference` (symbol): `costura.assemblytoreference.pdb.compressed` - `assemblytoreferencepreembedded`: `costura.assemblytoreferencepreembedded.dll` - `assemblytoreferencepreembedded` (symbol): `costura.assemblytoreferencepreembedded.pdb.compressed` - `exetoreference`: `costura.exetoreference.exe.compressed` - `exetoreference` (symbol): `costura.exetoreference.pdb.compressed` ``` -------------------------------- ### Include Runtimes by Element Source: https://github.com/fody/costura/blob/develop/readme.md Specify runtimes to include for embedding using the `IncludeRuntimes` element with newline-delimited values. Example values include 'win', 'win-x64', 'unix'. ```xml Foo Bar ```