Convert Figma logo to code with AI

jbevain logocecil

Cecil is a library to inspect, modify and create .NET programs and libraries.

2,710
620
2,710
105

Top Related Projects

11,073

Mono open source ECMA CLI, C# and .NET implementation.

5,147

A library for patching, replacing and decorating .NET and Mono methods during runtime

2,128

Reads and writes .NET assemblies and modules

6,889

.NET deobfuscator and unpacker.

14,915

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.

18,892

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.

Quick Overview

Cecil is a library to generate and inspect programs and libraries in the ECMA CIL (Common Intermediate Language) format. It's widely used for reading, writing, and manipulating .NET assemblies and modules, making it a powerful tool for developers working with .NET technologies.

Pros

  • Provides low-level access to .NET assemblies and modules
  • Supports both reading and writing of assemblies
  • Actively maintained with regular updates
  • Lightweight and efficient, with minimal dependencies

Cons

  • Steep learning curve for beginners due to its low-level nature
  • Documentation could be more comprehensive
  • Requires careful handling to avoid breaking assemblies
  • Limited support for some newer .NET features

Code Examples

  1. Reading an assembly:
using Mono.Cecil;

var assembly = AssemblyDefinition.ReadAssembly("path/to/assembly.dll");
foreach (var type in assembly.MainModule.Types)
{
    Console.WriteLine($"Type: {type.FullName}");
}
  1. Modifying a method:
var method = type.Methods.First(m => m.Name == "MethodName");
var il = method.Body.GetILProcessor();
il.InsertBefore(method.Body.Instructions[0], il.Create(OpCodes.Nop));
  1. Creating a new type:
var newType = new TypeDefinition("Namespace", "NewClass", TypeAttributes.Public | TypeAttributes.Class);
assembly.MainModule.Types.Add(newType);

var ctor = new MethodDefinition(".ctor", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, assembly.MainModule.TypeSystem.Void);
newType.Methods.Add(ctor);

Getting Started

  1. Install Cecil via NuGet:

    dotnet add package Mono.Cecil
    
  2. Basic usage:

    using Mono.Cecil;
    
    // Read an assembly
    var assembly = AssemblyDefinition.ReadAssembly("MyAssembly.dll");
    
    // Modify the assembly
    // ...
    
    // Write the modified assembly
    assembly.Write("ModifiedAssembly.dll");
    

Remember to handle exceptions and dispose of resources properly in production code.

Competitor Comparisons

11,073

Mono open source ECMA CLI, C# and .NET implementation.

Pros of Mono

  • Broader scope: Mono is a complete open-source implementation of .NET Framework, offering a more comprehensive ecosystem
  • Active development: Larger community and more frequent updates
  • Cross-platform support: Runs on various operating systems, including Linux and macOS

Cons of Mono

  • Larger codebase: More complex and potentially harder to navigate for specific tasks
  • Higher resource requirements: Consumes more system resources due to its comprehensive nature
  • Steeper learning curve: Requires more time to understand and contribute effectively

Code Comparison

Cecil:

AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly("MyAssembly.dll");
TypeDefinition type = assembly.MainModule.Types.First(t => t.Name == "MyClass");
MethodDefinition method = type.Methods.First(m => m.Name == "MyMethod");

Mono:

Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
Type type = assembly.GetType("MyNamespace.MyClass");
MethodInfo method = type.GetMethod("MyMethod");

Both examples demonstrate loading an assembly and accessing a specific method, but Cecil provides more low-level access to the assembly structure, while Mono uses the standard .NET reflection API.

5,147

A library for patching, replacing and decorating .NET and Mono methods during runtime

Pros of Harmony

  • Designed specifically for runtime patching and modding
  • Higher-level API, easier to use for non-experts
  • Supports patching methods at runtime without modifying assemblies on disk

Cons of Harmony

  • Limited to runtime patching, less flexible for static analysis
  • May have performance overhead due to runtime modifications
  • Less suitable for general-purpose IL manipulation tasks

Code Comparison

Cecil:

var assembly = AssemblyDefinition.ReadAssembly("MyAssembly.dll");
var type = assembly.MainModule.GetType("MyNamespace.MyClass");
var method = type.Methods.First(m => m.Name == "MyMethod");
method.Body.Instructions.Clear();
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
assembly.Write("ModifiedAssembly.dll");

Harmony:

var harmony = new Harmony("com.example.patch");
var original = typeof(MyClass).GetMethod("MyMethod");
var prefix = typeof(MyPatch).GetMethod("Prefix");
harmony.Patch(original, new HarmonyMethod(prefix));

Summary

Cecil is a powerful library for IL manipulation and static analysis, offering fine-grained control over assemblies. Harmony, on the other hand, focuses on runtime patching and modding, providing a simpler API for method interception. Choose Cecil for low-level IL manipulation and static analysis, and Harmony for runtime patching and game modding scenarios.

2,128

Reads and writes .NET assemblies and modules

Pros of dnlib

  • Faster parsing and writing of .NET assemblies
  • More comprehensive support for obfuscated assemblies
  • Better handling of malformed metadata

Cons of dnlib

  • Less mature and less widely used than Cecil
  • Smaller community and fewer resources available
  • Limited documentation compared to Cecil

Code Comparison

Cecil:

var assembly = AssemblyDefinition.ReadAssembly("path/to/assembly.dll");
var type = assembly.MainModule.GetType("Namespace.ClassName");
var method = type.Methods.First(m => m.Name == "MethodName");

dnlib:

var module = ModuleDefMD.Load("path/to/assembly.dll");
var type = module.Find("Namespace.ClassName", false);
var method = type.FindMethod("MethodName");

Both libraries provide similar functionality for reading and manipulating .NET assemblies. Cecil has a more established ecosystem and better documentation, making it easier for beginners. dnlib, on the other hand, offers better performance and handling of obfuscated code, which can be crucial for certain applications, especially in reverse engineering scenarios.

The choice between Cecil and dnlib depends on the specific requirements of your project, such as performance needs, the complexity of the assemblies you're working with, and the level of community support you require.

6,889

.NET deobfuscator and unpacker.

Pros of de4dot

  • Specialized tool for deobfuscating .NET assemblies
  • Includes advanced features for handling various obfuscation techniques
  • Actively maintained with regular updates

Cons of de4dot

  • More limited in scope compared to Cecil's general-purpose functionality
  • Steeper learning curve for users not familiar with deobfuscation techniques
  • May require additional tools for comprehensive .NET assembly manipulation

Code Comparison

Cecil:

var assembly = AssemblyDefinition.ReadAssembly("path/to/assembly.dll");
var type = assembly.MainModule.GetType("Namespace.ClassName");
var method = type.Methods.First(m => m.Name == "MethodName");

de4dot:

var options = new DeobfuscatorOptions();
var deobfuscator = new DeobfuscatorInfo(options).CreateDeobfuscator();
var file = new ObfuscatedFile(options, "path/to/assembly.dll");
deobfuscator.DeobfuscateBegin();

Summary

Cecil is a general-purpose library for reading, writing, and manipulating .NET assemblies, offering a wide range of functionality for working with IL code. de4dot, on the other hand, is a specialized tool focused on deobfuscating .NET assemblies, with advanced features for handling various obfuscation techniques. While Cecil provides more flexibility for general assembly manipulation, de4dot excels in its specific use case of deobfuscation.

14,915

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.

Pros of runtime

  • Comprehensive .NET runtime implementation with extensive features and optimizations
  • Official Microsoft repository with active development and support
  • Large community and ecosystem, frequent updates and improvements

Cons of runtime

  • Complex codebase with a steep learning curve for contributors
  • Heavier and more resource-intensive compared to Cecil's focused approach
  • May include unnecessary components for specific use cases

Code Comparison

Cecil:

AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly("MyAssembly.dll");
TypeDefinition type = assembly.MainModule.GetType("MyNamespace.MyClass");
MethodDefinition method = type.Methods.First(m => m.Name == "MyMethod");

runtime:

Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
Type type = assembly.GetType("MyNamespace.MyClass");
MethodInfo method = type.GetMethod("MyMethod");

Summary

Cecil is a lightweight library for reading, writing, and analyzing .NET assemblies, while runtime is the official .NET runtime implementation. Cecil offers simplicity and focused functionality for assembly manipulation, making it ideal for specific tasks. runtime provides a complete .NET environment with broader capabilities but comes with increased complexity. The choice between them depends on the project's requirements and scope.

18,892

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.

Pros of Roslyn

  • More comprehensive analysis and compilation capabilities for C# and VB.NET
  • Extensive API for code generation, refactoring, and syntax manipulation
  • Actively maintained by Microsoft with frequent updates and improvements

Cons of Roslyn

  • Larger and more complex codebase, potentially steeper learning curve
  • Higher resource consumption due to its comprehensive feature set
  • May be overkill for simpler code analysis or manipulation tasks

Code Comparison

Cecil:

var assembly = AssemblyDefinition.ReadAssembly("MyAssembly.dll");
var type = assembly.MainModule.Types.First(t => t.Name == "MyClass");
var method = type.Methods.First(m => m.Name == "MyMethod");

Roslyn:

var tree = CSharpSyntaxTree.ParseText(sourceCode);
var root = tree.GetRoot();
var classDeclaration = root.DescendantNodes().OfType<ClassDeclarationSyntax>().First(c => c.Identifier.Text == "MyClass");
var methodDeclaration = classDeclaration.DescendantNodes().OfType<MethodDeclarationSyntax>().First(m => m.Identifier.Text == "MyMethod");

Cecil is more focused on IL manipulation and analysis, while Roslyn provides a richer set of tools for source code analysis and manipulation. Cecil is lighter and may be preferred for simpler tasks, while Roslyn offers more advanced features at the cost of increased complexity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Cecil

Mono.Cecil is a library to generate and inspect programs and libraries in the ECMA CIL form.

To put it simply, you can use Cecil to:

  • Analyze .NET binaries using a simple and powerful object model, without having to load assemblies to use Reflection.
  • Modify .NET binaries, add new metadata structures and alter the IL code.

Cecil has been around since 2004 and is widely used in the .NET community. If you're using Cecil, or depend on a framework, project, or product using it, please consider sponsoring Cecil.

To discuss Cecil, the best place is the mono-cecil Google Group.

Cecil is a project under the benevolent umbrella of the .NET Foundation.