How Glass.NET Restores Typed .NET Resources
Summary
Pixel shares a DEV Community walkthrough of how Glass.NET recognizes the regular structure of generated Resources.Designer.cs classes in compiled .NET assemblies. It explains how the decompiler reconstructs strongly typed resource accessors and resource keys from IL, making localized applications easier to inspect and understand.
DEV Community
Recovering the strongly-typed Resources designer with Glass.NET
Open almost any localized .NET application and you will find a file nobody wrote by hand: Resources.Designer.cs. Visual Studio's ResXFileCodeGenerator produces it from a .resx file, and it gives you Resources.Greeting instead of a stringly-typed resourceManager.GetString("Greeting") scattered through your code. It is pure convenience glue — a lazily-initialized ResourceManager, a culture field, and one property per resource key. When you decompile an assembly, that generated class is just another type in the metadata. There is no marker that says "this was designer-generated," no .resx embedded as source. But the shape is extremely regular, and that regularity is exactly what Glass.NET, the free .NET decompiler, keys on to hand you back a readable designer class instead of a pile of GetString calls. This post walks one resource accessor from source to IL and back, and shows how Glass recovers both the typed class and the resource keys behind it. The source Start with a two-entry .resx. Conceptually it is just a key/value table: Name Value Greeting Hello, {0}! Farewell Goodbye. The generator turns that into a class. Here is the relevant core of what ResXFileCodeGenerator emits (trimmed to two members, fully-qualified names collapsed for readability): [System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[System.Diagnostics.DebuggerNonUserCodeAttribute]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute]
internal class Resources
{
private static System.Resources.ResourceManager resourceMan;
private static System.Globalization.CultureInfo resourceCulture;
internal static System.Resources.ResourceManager ResourceManager
{
get
{
if (object.ReferenceEquals(resourceMan, null))
{
var temp = new System.Resources.ResourceManager(
"MyApp.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
internal static System.Globalization.CultureInfo Culture
{
get { return resourceCulture; }
set { resourceCulture = value; }
}
internal static string Greeting =>
ResourceManager.GetString("Greeting", resourceCulture);
}
Two parts matter for reconstruction: the lazy ResourceManager getter (a null check that constructs the manager once and caches it) and the typed property (Greeting), which is nothing but a forwarded GetString keyed by the property's own name. What the compiler emits The typed property is the interesting one, because it is the piece that repeats once per key and the piece a reader most wants collapsed. Here is get_Greeting as ildasm/Glass shows it: .method assembly hidebysig specialname static
string get_Greeting () cil managed
{
.maxstack 8
IL_0000: call class [System.Resources]System.Resources.ResourceManager Resources::get_ResourceManager()
IL_0005: ldstr "Greeting"
IL_000a: ldsfld class [System.Globalization]System.Globalization.CultureInfo Resources::resourceCulture
IL_000f: callvirt instance string [System.Resources]System.Resources.ResourceManager::GetString(string, class [System.Globalization]System.Globalization.CultureInfo)
IL_0014: ret
}
Read it straight through: call the ResourceManager property, push the literal key "Greeting", push the static resourceCulture field, call GetString(string, CultureInfo), return the result. The key string in the ldstr is identical to the property name — that equality is the heart of the fingerprint. The lazy getter lowers to the familiar check-and-cache: .method assembly hidebysig specialname static
class [System.Resources]System.Resources.ResourceManager get_ResourceManager () cil managed
{
.maxstack 8
.locals init (class [System.Resources]System.Resources.ResourceManager V_0)
IL_0000: ldsfld class ...ResourceManager Resources::res...

Powered by Readybot.io