🔧 C#: Fix an Init-Only Setter Bypassed by Reflection
An init accessor is a compile-time construct — the C# compiler enforces that it can only be called from within an object initializer expression or a constructor, by emitting an IsExternalInit modifier the compiler itself checks for at the call site. That restriction exists nowhere in the actual IL method signature the runtime sees: an init-only property’s setter is just an ordinary set_PropertyName method with a modreq attached, and reflection APIs like PropertyInfo.SetValue don’t inspect or enforce modreqs at all, so any code calling through reflection can mutate a property the type’s own source code guarantees is only ever set once, completely silently, with no exception and no warning that the type’s intended immutability contract has just been violated.
🔎 The Problem
public class Order
{
public int Id { get; init; }
public decimal Total { get; init; }
}
var order = new Order { Id = 1, Total = 99.99m };
// Later, somewhere else in the codebase (a generic deserializer,
// a test helper, a mapping library using reflection under the hood):
typeof(Order)
.GetProperty(nameof(Order.Total))!
.SetValue(order, 0m);
Console.WriteLine(order.Total);
// 0 - the "immutable" record was mutated after construction with
// no compile error, no runtime exception, and no visible trace of it
// unless you already suspect reflection is involved somewhere
✅ Fix: Don’t Trust init Alone Where True Immutability Matters
- Treat
initas a compile-time authoring convenience, not a runtime guarantee — if a type’s immutability is load-bearing for correctness (a cache key, a value used in a hash set, anything shared across threads), back it with areadonlyfield set only in the constructor, which reflection genuinely cannot bypass the same way. - Audit any library or framework code in the project that uses reflection-based property setting (generic object mappers, certain serializers, test double/mocking frameworks) for whether it’s being pointed at init-only properties on types where that mutation would actually be a problem, since the capability existing in a dependency doesn’t mean it’s being exercised against your sensitive types specifically.
- For types where this risk is realistic, add a unit test that explicitly attempts the reflection-based mutation and asserts it either throws or is caught by a code analyzer rule, turning an invisible footgun into a visible, intentional regression test instead of hoping nobody ever reaches for reflection near that type.
🚨 Why This Is Dangerous
- A value object mutated after construction via reflection can silently invalidate any invariant the rest of the codebase assumes holds for that type — a hash code computed from a now-stale field, a cache keyed by a value that’s since changed underneath it — and because nothing throws, the resulting bug surfaces far away from the actual mutation, as a hard-to-explain data inconsistency rather than an obvious crash.
init-only properties are enforced by the compiler that writes your code, not by the runtime that executes it — and reflection talks to the runtime directly, which means it was never actually bound by that promise in the first place.





