louthy/language-ext · error · ArgumentException

ToString method found for StringBuilder

Error message

ToString method found for StringBuilder

What it means

Sentinel guard in ToStringExpr<A>: reflection could not resolve StringBuilder.ToString() while assembling the expression tree, so the generated string-producing lambda would be invalid. Fires only if the core reflection metadata is unavailable; it is a fail-fast abort, not input validation.

Solutions

  1. Handle the exception where the Show instance is derived and substitute a fallback string conversion (default ToString or a hand-written formatter) for that type.
  2. Ensure trimming/AOT settings preserve object.ToString and related members used by the expression builder, or disable trimming for the affected assembly.
  3. Identify and log the failing type A, then provide an explicit Show/ToString registration for it so the dynamic compilation path is bypassed.
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at LanguageExt.Core/Utility/IL.cs:782 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/46fdd309be505ec0. Report an issue: GitHub.

Appendix: source

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

               .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());

            return Expression.Block(

View on GitHub (pinned to 2f0e362824)