louthy/language-ext · critical · ArgumentException

ToString method found for Object

Error message

ToString method found for Object

What it means

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.

Solutions

  1. Run on an unmodified, standard .NET runtime — reinstall/repair if the runtime is suspect
  2. Exclude corelib from trimming or add a preserve-all rule for System.Object
  3. Check that nothing (weavers, IL post-processors) rewrites mscorlib/System.Private.CoreLib in the deployment
  4. Prefer LanguageExt's expression-tree fallback (ILCapability.Available == false) or a hand-written ToString override

Example fix

// verify runtime sanity before using IL-based features
var ok = typeof(object).GetMethod("ToString", Type.EmptyTypes) != null;
if (!ok) throw new InvalidOperationException("Runtime corelib is stripped/corrupt; IL features unavailable");
Defensive patterns

Strategy: type-guard

Validate before calling

var toStringOk = typeof(object).GetMethod("ToString", Type.EmptyTypes) != null;
if (!toStringOk) throw new InvalidOperationException("Core runtime reflection surface is broken; reinstall/repair the .NET runtime before using IL features");

Type guard

static bool RuntimeIlSafe =>
    typeof(object).GetMethod("ToString", Type.EmptyTypes) != null &&
    typeof(StringBuilder).GetConstructor(Type.EmptyTypes) != null;

Try / catch

try { return IL.ToString<A>(includeBase); }
catch (ArgumentException ex) when (ex.Message.Contains("ToString method"))
{ return a => a.ToString() ?? ""; }

Prevention

When it happens

Trigger: 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.

Common situations: Extreme linker configurations stubbing object methods; custom or corrupt runtime installations; unit-test weavers that rewrite corelib metadata.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15). Data as JSON: /api/errors/931e861dd2634135. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Core/Utility/IL.cs:858

        {
            return ToStringExpr<A>(includeBase);
        }

        var isValueType = typeof(A).GetTypeInfo().IsValueType;
        var dynamic = new DynamicMethod("FieldsToString", 
                                        typeof(string),
                                        [typeof(A)],                                            
                                        typeof(A).Module,
                                        true);
        var fields = GetPublicInstanceFields<A>(
            includeBase,
            typeof(NonShowAttribute),
            typeof(NonRecordAttribute)
        ).ToArray();
        var stringBuilder = GetConstructor<StringBuilder>().IfNone(() => throw new ArgumentException($"Constructor not found for StringBuilder"));
        var appendChar = GetPublicInstanceMethod<StringBuilder, char>("Append", true).IfNone(() => throw new ArgumentException($"Append method found for StringBuilder"));
        var appendString = GetPublicInstanceMethod<StringBuilder, string>("Append", true).IfNone(() => throw new ArgumentException($"Append method found for StringBuilder"));
        var toString = GetPublicInstanceMethod<Object>("ToString", true).IfNone(() => throw new ArgumentException($"ToString method found for Object"));
        var name = typeof(A).Name;
        if (name.IndexOf('`') != -1) name = name.Split('`').AsIterable().Head.Value!;

        var il = dynamic.GetILGenerator();
        il.DeclareLocal(typeof(StringBuilder));
        var notNull = il.DefineLabel();

        if (!isValueType)
        {
            // Check reference == null
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Brtrue_S, notNull);

            // Is null so return "(null)"
            il.Emit(OpCodes.Ldstr, "(null)");
            il.Emit(OpCodes.Ret);

            il.MarkLabel(notNull);

View on GitHub (pinned to 2f0e362824)