### Creating Array Types and Converting TypeSig/ITypeDefOrRef in C# Source: https://github.com/0xd4d/dnlib/blob/master/README.md This snippet demonstrates how to create different array types using dnlib's TypeSig classes (SZArraySig, ArraySig) and how to convert between TypeSig and ITypeDefOrRef using extension methods. It shows examples for byte[], int[][], int[,], int[*], and Stream[], highlighting the use of CorLibTypes and wrapping reference types in ClassSig. ```C# ModuleDef mod = ....; // Create a byte[] SZArraySig array1 = new SZArraySig(mod.CorLibTypes.Byte); // Create an int[][] SZArraySig array2 = new SZArraySig(new SZArraySig(mod.CorLibTypes.Int32)); // Create an int[,] ArraySig array3 = new ArraySig(mod.CorLibTypes.Int32, 2); // Create an int[*] (one-dimensional array) ArraySig array4 = new ArraySig(mod.CorLibTypes.Int32, 1); // Create a Stream[]. Stream is a reference class so it must be enclosed in a ClassSig. // If it were a value type, you would use ValueTypeSig instead. TypeRef stream = new TypeRefUser(mod, "System.IO", "Stream", mod.CorLibTypes.AssemblyRef); SZArraySig array5 = new SZArraySig(new ClassSig(stream)); // array5 is defined above ITypeDefOrRef type1 = array5.ToTypeDefOrRef(); TypeSig type2 = type1.ToTypeSig(); ``` -------------------------------- ### Initializing Module Context and Assembly Resolver (C#) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Shows how to create a `ModuleContext` and obtain the default `AssemblyResolver`. It explains that the context should be shared between all modules and demonstrates enabling the `TypeDef` cache for loaded assemblies. ```C# // You should pass this context to ModuleDefMD.Load(), but you can also write // it to `module.Context` ModuleContext modCtx = ModuleDef.CreateModuleContext(); // It creates the default assembly resolver AssemblyResolver asmResolver = (AssemblyResolver)modCtx.AssemblyResolver; // Enable the TypeDef cache for all assemblies that are loaded // by the assembly resolver. Only enable it if all auto-loaded // assemblies are read-only. asmResolver.EnableTypeDefCache = true; ``` -------------------------------- ### Enhanced Strong Name Sign Assembly with dnlib (With Key Migration) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Shows how to perform enhanced strong name signing with key migration. This process requires both signature and identity key pairs to initialize the writer options and add the necessary attribute. ```C# using dnlib.DotNet.Writer; ... // Open or create an assembly ModuleDef mod = ModuleDefMD.Load(....); // Open or create the identity and signature keys var signatureKey = new StrongNameKey(....); var signaturePubKey = new StrongNamePublicKey(....); var identityKey = new StrongNameKey(....); var identityPubKey = new StrongNamePublicKey(....); // Create module writer options var opts = new ModuleWriterOptions(mod); // This method will initialize the required properties and add // the required attribute to the assembly. opts.InitializeEnhancedStrongNameSigning(mod, signatureKey, signaturePubKey, identityKey, identityPubKey); // Write and strong name sign the assembly mod.Write(@"C:\out\file.dll", opts); ``` -------------------------------- ### Loading .NET Module from File (ModuleDefMD) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Demonstrates how to load a .NET module or assembly from a file path using ModuleDefMD.Load(). Requires creating a ModuleContext. ```C# // Create a default assembly resolver and type resolver and pass it to Load(). // If it's a .NET Core assembly, you'll need to disable GAC loading and add // .NET Core reference assembly search paths. ModuleContext modCtx = ModuleDef.CreateModuleContext(); ModuleDefMD module = ModuleDefMD.Load(@"C:\path\to\file.exe", modCtx); ``` -------------------------------- ### Loading .NET Module from Reflection Module (ModuleDefMD) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Illustrates loading a .NET module from an existing System.Reflection.Module instance using ModuleDefMD.Load(). Useful for inspecting already loaded assemblies. ```C# System.Reflection.Module reflectionModule = typeof(void).Module; // Get mscorlib.dll's module // See comment above about the assembly resolver ModuleContext modCtx = ModuleDef.CreateModuleContext(); ModuleDefMD module = ModuleDefMD.Load(reflectionModule, modCtx); ``` -------------------------------- ### Strong Name Sign Assembly with dnlib (Basic) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Use this code to strong name sign an assembly when saving it using a strong name key file. It initializes the necessary writer options and writes the signed assembly. ```C# using dnlib.DotNet.Writer; ... // Open or create an assembly ModuleDef mod = ModuleDefMD.Load(.....); // Create writer options var opts = new ModuleWriterOptions(mod); // Open or create the strong name key var signatureKey = new StrongNameKey(@"c:\my\file.snk"); // This method will initialize the required properties opts.InitializeStrongNameSigning(mod, signatureKey); // Write and strong name sign the assembly mod.Write(@"C:\out\file.dll", opts); ``` -------------------------------- ### Enhanced Strong Name Sign Assembly with dnlib (No Key Migration) Source: https://github.com/0xd4d/dnlib/blob/master/README.md This snippet demonstrates enhanced strong name signing without key migration. It requires both a signature key and a signature public key to initialize the module writer options before writing the assembly. ```C# using dnlib.DotNet.Writer; ... // Open or create an assembly ModuleDef mod = ModuleDefMD.Load(....); // Open or create the signature keys var signatureKey = new StrongNameKey(....); var signaturePubKey = new StrongNamePublicKey(....); // Create module writer options var opts = new ModuleWriterOptions(mod); // This method will initialize the required properties opts.InitializeEnhancedStrongNameSigning(mod, signatureKey, signaturePubKey); // Write and strong name sign the assembly mod.Write(@"C:\out\file.dll", opts); ``` -------------------------------- ### Comparing Types with SigComparer and TypeEqualityComparer (C#) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Demonstrates how to use `SigComparer` to compare two `IType` instances (like `TypeRef` and `TypeDef`) and how to use `TypeEqualityComparer.Instance` when using `IType` as a dictionary key. Also shows comparing a `TypeRef` with a `System.Type`. ```C# // Compare two types TypeRef type1 = ...; TypeDef type2 = ...; if (new SigComparer().Equals(type1, type2)) Console.WriteLine("They're equal"); // Use the type equality comparer Dictionary dict = new Dictionary(TypeEqualityComparer.Instance); TypeDef type1 = ...; dict.Add(type1, 10); // Compare a `TypeRef` with a `System.Type` TypeRef type1 = ...; if (new SigComparer().Equals(type1, typeof(int))) Console.WriteLine("They're equal"); ``` -------------------------------- ### Loading .NET Module from Byte Array (ModuleDefMD) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Shows how to load a .NET module or assembly from a byte array in memory using ModuleDefMD.Load(). Requires reading the file into a byte array first. ```C# byte[] data = System.IO.File.ReadAllBytes(@"C:\path\of\file.dll"); // See comment above about the assembly resolver ModuleContext modCtx = ModuleDef.CreateModuleContext(); ModuleDefMD module = ModuleDefMD.Load(data, modCtx); ``` -------------------------------- ### Saving Module with PDB File Source: https://github.com/0xd4d/dnlib/blob/master/README.md Illustrates how to save a .NET module along with its PDB debug information by configuring ModuleWriterOptions to write the PDB. ```C# // Create a default assembly resolver and type resolver ModuleContext modCtx = ModuleDef.CreateModuleContext(); var mod = ModuleDefMD.Load(@"C:\myfile.dll", modCtx); // ... var wopts = new dnlib.DotNet.Writer.ModuleWriterOptions(mod); wopts.WritePdb = true; // wopts.PdbFileName = @"C:\out2.pdb"; // Set other file name mod.Write(@"C:\out.dll", wopts); ``` -------------------------------- ### Associating Module with Context and Adding to Resolver Cache (C#) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Illustrates how to assign a previously created `ModuleContext` to a loaded `ModuleDefMD` instance. It also shows how to add the loaded module to the assembly resolver's cache, which is necessary for resolving references within that module. ```C# ModuleDefMD mod = ModuleDefMD.Load(...); mod.Context = modCtx; // Use the previously created (and shared) context // This code assumes you're using the default assembly resolver ((AssemblyResolver)mod.Context.AssemblyResolver).AddToCache(mod); ``` -------------------------------- ### Importing Runtime Type with Importer (C#) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Explains how to use the `Importer` class to convert a runtime `System.Type` (like `typeof(System.Console)`) into a dnlib `ITypeDefOrRef` that can be used within the module's metadata. ```C# Importer importer = new Importer(mod); ITypeDefOrRef consoleRef = importer.Import(typeof(System.Console)); ``` -------------------------------- ### Importing dnlib Namespaces Source: https://github.com/0xd4d/dnlib/blob/master/README.md Imports the necessary namespaces from the dnlib library to work with .NET modules, assemblies, and method bodies. ```C# using dnlib.DotNet; using dnlib.DotNet.Emit; ``` -------------------------------- ### Saving C++/CLI Module to File (module.NativeWrite) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Shows how to save a C++/CLI assembly, which contains native code, using the module.NativeWrite() method. ```C# module.NativeWrite(@"C:\saved-assembly.dll"); ``` -------------------------------- ### Saving .NET Module to File (module.Write) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Demonstrates saving a modified .NET module or assembly back to a file using the module.Write() method. ```C# module.Write(@"C:\saved-assembly.dll"); ``` -------------------------------- ### Importing Method Reference using dnlib C# Source: https://github.com/0xd4d/dnlib/blob/master/README.md This snippet demonstrates how to import a reference to a method from another module or assembly using an `importer` object in dnlib. It specifically imports the `WriteLine` method from `System.Console`. ```C# IMethod writeLine = importer.Import(typeof(System.Console).GetMethod("WriteLine")); ``` -------------------------------- ### Creating TypeRef for Standard Library Type (C#) Source: https://github.com/0xd4d/dnlib/blob/master/README.md Shows how to manually instantiate a `TypeRefUser` for a standard library type like `System.Console` when it's not available directly via `mod.CorLibTypes`. It requires the module, namespace, name, and the assembly reference for the core library. ```C# TypeRef consoleRef = new TypeRefUser(mod, "System", "Console", mod.CorLibTypes.AssemblyRef); ``` -------------------------------- ### Accessing Assembly from ModuleDefMD Source: https://github.com/0xd4d/dnlib/blob/master/README.md Explains how to retrieve the AssemblyDef instance associated with a loaded ModuleDefMD instance using the Assembly property. ```C# AssemblyDef asm = module.Assembly; Console.WriteLine("Assembly: {0}", asm); ``` -------------------------------- ### Accessing TypeDef RIDs from Metadata in dnlib C# Source: https://github.com/0xd4d/dnlib/blob/master/README.md This C# snippet demonstrates how to access the low-level metadata of a loaded module (`ModuleDefMD`) in dnlib to retrieve a list of Type Definition Row IDs (Rids). It iterates through the list and prints each Rid. ```C# using dnlib.DotNet.MD; // ... ModuleDefMD mod = ModuleDefMD.Load(...); RidList typeDefRids = mod.Metadata.GetTypeDefRidList(); for (int i = 0; i < typeDefRids.Count; i++) Console.WriteLine("rid: {0}", typeDefRids[i]); ``` -------------------------------- ### Saving Module Based on IL-Only Status Source: https://github.com/0xd4d/dnlib/blob/master/README.md Provides code to detect if a module contains only IL code or includes native code (like C++/CLI) and saves it using the appropriate Write or NativeWrite method. ```C# if (module.IsILOnly) { // This assembly has only IL code, and no native code (eg. it's a C# or VB assembly) module.Write(@"C:\saved-assembly.dll"); } else { // This assembly has native code (eg. C++/CLI) module.NativeWrite(@"C:\saved-assembly.dll"); } ``` -------------------------------- ### Change Method Calling Convention for Export with dnlib Source: https://github.com/0xd4d/dnlib/blob/master/README.md To export a managed method for native interop, its calling convention must be changed. This code demonstrates how to add a calling convention modifier (like Cdecl) to the method's return type signature. ```C# var type = method.MethodSig.RetType; type = new CModOptSig(module.CorLibTypes.GetTypeRef("System.Runtime.CompilerServices", "CallConvCdecl"), type); method.MethodSig.RetType = type; ``` -------------------------------- ### Workaround for VS Debugger Crash with DebuggableAttribute in dnlib Source: https://github.com/0xd4d/dnlib/blob/master/README.md Provides a workaround for a Visual Studio debugger crash that occurs when a DebuggableAttribute has a specific constructor argument value (0x107). The code clears the EnableEditAndContinue bit in the attribute's value. ```C# var ca = module.Assembly.CustomAttributes.Find("System.Diagnostics.DebuggableAttribute"); if (ca is not null && ca.ConstructorArguments.Count == 1) { var arg = ca.ConstructorArguments[0]; // VS' debugger crashes if value == 0x107, so clear EnC bit if (arg.Type.FullName == "System.Diagnostics.DebuggableAttribute/DebuggingModes" && arg.Value is int value && value == 0x107) { arg.Value = value & ~(int)DebuggableAttribute.DebuggingModes.EnableEditAndContinue; ca.ConstructorArguments[0] = arg; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.