il-copy-inspection

Inspect IL to locate value-type struct copies and map them to source lines.

9|1|Updated Jun 6, 2025
One-click install
npx skills add https://github.com/JeremyKuhne/touki --skill il-copy-inspection
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: il-copy-inspection
Source: https://github.com/JeremyKuhne/touki/tree/main/.agents/skills/il-copy-inspection
Command: npx skills add https://github.com/JeremyKuhne/touki --skill il-copy-inspection

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Find how the compiler emits copies of value-type structs in IL, including defensive copies and boxing, to ground-truth compiler behavior beyond source code.

Core Features & Use Cases

  • IL-level copy detection: Identify IL opcodes that copy struct values (ldobj, stobj, cpobj, box, unbox.any, ldfld for value types) and map them to source lines.
  • Post-build verification: Provide ground-truth for source-level defensive-copy rules by correlating IL copies with the original C# code.
  • Use Case: Audit a readonly invocation on a struct to determine if the compiler emitted a defensive copy and where it occurs in the IL.

Quick Start

Inspect the IL of a method to locate where a struct is copied during a call

Frequently Asked Questions about il-copy-inspection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I detect defensive copies of structs in IL?

Detect defensive copies of structs in IL by inspecting compiled method bodies with tools like ilspycmd or ildasm to locate copy opcodes such as ldobj, stobj, and cpobj, then mapping those IL offsets back to the original C# source lines using a portable PDB.

Why does the C# compiler emit a defensive copy on a readonly struct field?

The C# compiler emits a defensive copy on a readonly struct field to preserve value-type semantics when invoking methods, creating a hidden copy in the IL that is not visible at the source level and can be verified by inspecting the method body.

How do I inspect IL opcodes for struct boxing and copies?

Inspect IL opcodes for struct boxing and copies by disassembling the compiled assembly using Mono.Cecil or System.Reflection.Metadata to identify box, unbox.any, and ldfld instructions, revealing the exact compiler behavior applied to value types.

Do I need a portable PDB to map IL copies to C# source lines?

Yes, you need a portable PDB to map IL copies to C# source lines because it provides the sequence point data required to correlate the detected IL copy opcodes with their exact locations in the original C# methods.

Can I use ilspycmd to verify in parameter struct modifications in IL?

Yes, you can use ilspycmd to verify in parameter struct modifications in IL by disassembling the compiled assembly and examining the emitted opcodes to determine if the compiler generated hidden defensive copies during the method call.

What IL opcodes indicate a value-type struct has been copied?

The IL opcodes that indicate a value-type struct has been copied include ldobj, stobj, cpobj, box, unbox.any, and ldfld for value types, which collectively reveal defensive copies and boxing operations emitted by the compiler.