{"record":{"id":"931e861dd2634135","repo":"louthy/language-ext","slug":"tostring-method-found-for-object","errorCode":null,"errorMessage":"ToString method found for Object","messagePattern":"ToString method found for Object","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"critical","filePath":"LanguageExt.Core/Utility/IL.cs","lineNumber":858,"sourceCode":"        {\n            return ToStringExpr<A>(includeBase);\n        }\n\n        var isValueType = typeof(A).GetTypeInfo().IsValueType;\n        var dynamic = new DynamicMethod(\"FieldsToString\", \n                                        typeof(string),\n                                        [typeof(A)],                                            \n                                        typeof(A).Module,\n                                        true);\n        var fields = GetPublicInstanceFields<A>(\n            includeBase,\n            typeof(NonShowAttribute),\n            typeof(NonRecordAttribute)\n        ).ToArray();\n        var stringBuilder = GetConstructor<StringBuilder>().IfNone(() => throw new ArgumentException($\"Constructor not found for StringBuilder\"));\n        var appendChar = GetPublicInstanceMethod<StringBuilder, char>(\"Append\", true).IfNone(() => throw new ArgumentException($\"Append method found for StringBuilder\"));\n        var appendString = GetPublicInstanceMethod<StringBuilder, string>(\"Append\", true).IfNone(() => throw new ArgumentException($\"Append method found for StringBuilder\"));\n        var toString = GetPublicInstanceMethod<Object>(\"ToString\", true).IfNone(() => throw new ArgumentException($\"ToString method found for Object\"));\n        var name = typeof(A).Name;\n        if (name.IndexOf('`') != -1) name = name.Split('`').AsIterable().Head.Value!;\n\n        var il = dynamic.GetILGenerator();\n        il.DeclareLocal(typeof(StringBuilder));\n        var notNull = il.DefineLabel();\n\n        if (!isValueType)\n        {\n            // Check reference == null\n            il.Emit(OpCodes.Ldarg_0);\n            il.Emit(OpCodes.Brtrue_S, notNull);\n\n            // Is null so return \"(null)\"\n            il.Emit(OpCodes.Ldstr, \"(null)\");\n            il.Emit(OpCodes.Ret);\n\n            il.MarkLabel(notNull);","sourceCodeStart":840,"sourceCodeEnd":876,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Utility/IL.cs#L840-L876","documentation":"IL.ToString<A> finishes its reflection setup by resolving Object.ToString via GetPublicInstanceMethod<Object>(\"ToString\", true); if None it throws ArgumentException(\"ToString method found for Object\") (i.e. the method was not found) at LanguageExt.Core/Utility/IL.cs:858. Object.ToString is part of every CLR type, so this only occurs when the reflection surface is fundamentally broken or stripped — the emitted ToString call could not be bound.","triggerScenarios":"Calling IL.ToString<A> when object.ToString() cannot be resolved by reflection — heavily trimmed/stubbed core library, hostile instrumentation, or a non-standard runtime lacking the member.","commonSituations":"Extreme linker configurations stubbing object methods; custom or corrupt runtime installations; unit-test weavers that rewrite corelib metadata.","solutions":["Run on an unmodified, standard .NET runtime — reinstall/repair if the runtime is suspect","Exclude corelib from trimming or add a preserve-all rule for System.Object","Check that nothing (weavers, IL post-processors) rewrites mscorlib/System.Private.CoreLib in the deployment","Prefer LanguageExt's expression-tree fallback (ILCapability.Available == false) or a hand-written ToString override"],"exampleFix":"// verify runtime sanity before using IL-based features\nvar ok = typeof(object).GetMethod(\"ToString\", Type.EmptyTypes) != null;\nif (!ok) throw new InvalidOperationException(\"Runtime corelib is stripped/corrupt; IL features unavailable\");","handlingStrategy":"type-guard","validationCode":"var toStringOk = typeof(object).GetMethod(\"ToString\", Type.EmptyTypes) != null;\nif (!toStringOk) throw new InvalidOperationException(\"Core runtime reflection surface is broken; reinstall/repair the .NET runtime before using IL features\");","typeGuard":"static bool RuntimeIlSafe =>\n    typeof(object).GetMethod(\"ToString\", Type.EmptyTypes) != null &&\n    typeof(StringBuilder).GetConstructor(Type.EmptyTypes) != null;","tryCatchPattern":"try { return IL.ToString<A>(includeBase); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"ToString method\"))\n{ return a => a.ToString() ?? \"\"; }","preventionTips":["Never ship stripped/stubbed versions of System.Private.CoreLib or mscorlib","Audit linker/trimmer and IL-weaver configs so System.Object's base methods are always preserved","Repair or reinstall the .NET runtime if base-type reflection lookups fail","Gate IL-based LanguageExt features behind a runtime self-check at startup"],"tags":["reflection","trimming","object","dynamicmethod"],"backgroundTag":"missing-dependency","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}