Decompiling generic constraints and variance: reconstructing where-clauses and out/in from metadata
DEV Community
Decompiling generic constraints and variance: reconstructing where-clauses and out/in from metadata
Generic type-parameter constraints and variance are, like struct layout, features a decompiler cannot recover from code shapes — they live entirely in metadata. Each generic parameter carries a variance direction and special-constraint flags in the GenericParam table, and the GenericParamConstraint table lists the base type and interfaces a parameter is bound to. Covariance (out) and contravariance (in) are flag bits; the class / struct / new() constraints are flag bits too; but notnull and unmanaged are encoded indirectly, through a modreq and an attribute rather than a raw flag. Drop the variance and an interface stops being assignable the way it was; re-emit the constraints in the wrong order and the output does not compile at all. This post shows where the data lives, how Glass.NET reads the GenericParam and GenericParamConstraint tables, how it sorts the constraints into the exact order C# requires, and a worked covariant-interface plus multi-constraint-method example.
0 comments
No comments yet.