louthy/language-ext · error · ArgumentException

Append method found for StringBuilder

Error message

Append method found for StringBuilder

What it means

Sentinel guard in ToStringExpr<A>: the reflection lookup for StringBuilder.Append(char)/Append(string) returned None while compiling the ToString lambda for type A. The message is inverted ('found' means 'not found'); it signals an environment invariant failure, never a user-triggerable condition.

Solutions

  1. Catch the failure at Show-derivation time and fall back to a simple formatter that does not rely on expression compilation over StringBuilder.
  2. Check runtime trimming/AOT configuration: ensure StringBuilder's Append overloads are preserved so reflection-based expression building can find them.
  3. Pin or align the LanguageExt version with the runtime, since reflection member resolution can differ across .NET versions and break the generated expression.
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at LanguageExt.Core/Utility/IL.cs:780 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

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

                  }
               .Concat( Fields().Bind(identity))
               .Concat( new [] { Expression.Label(returnTarget, Zero) as Expression }));

        var lambda = Expression.Lambda<Func<A, A, int>>(block, self, other);

        return lambda.Compile();
    }

    static Func<A, string> ToStringExpr<A>(bool includeBase)
    {
        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<StringBuilder>("ToString", false).IfNone(() => throw new ArgumentException($"ToString method found for StringBuilder"));
        var name          = typeof(A).Name;
        var self          = Expression.Parameter(typeof(A), "self");
        var nullA         = Expression.Constant(null, typeof(A));
        var nullStr       = Expression.Constant(null, typeof(string));
        var sb            = Expression.Variable(typeof(StringBuilder), "sb");
        var tmpStr        = Expression.Variable(typeof(string), "tmpStr");
        var result        = Expression.Variable(typeof(string), "result");
        var returnTarget  = Expression.Label(typeof(string));
            
        if (name.IndexOf('`') != -1) name = name.Split('`').AsIterable().Head.Value!;

        Expression fieldExpr(FieldInfo field)
        {
            var convertToString = (GetPublicStaticMethod(typeof(Convert), "ToString", field.FieldType) ||
                                   GetPublicStaticMethod(typeof(Convert), "ToString", typeof(object)))
               .IfNone(() => throw new Exception());

View on GitHub (pinned to 2f0e362824)