simple-binary-encoding icon indicating copy to clipboard operation
simple-binary-encoding copied to clipboard

[C#] Avoid ToString() affect the message internal state

Open amibar opened this issue 10 months ago • 0 comments

Hi,

This PR intends to fix Issue 992.

The current generate ToString() implementation calls to this.BuildString() which change this state.

public override string ToString()
{
    var sb = new StringBuilder(100);
    this.BuildString(sb);
    return sb.ToString();
}

To avoid the affect ton this, I changed the generated ToString() to create a new instance of the message, wrap it and then use it with BuildString(). This way this is not affected.

public override string ToString()
{
    var sb = new StringBuilder(100);
    var m = new GroupAndVarLength();
    m.WrapForDecode(_buffer, _offset, _actingBlockLength, _actingVersion);
    m.BuildString(sb);
    return sb.ToString();
}

The assumption is that ToString() is only called for logging and debugging purposes, so the extra object created doesn't affect performance.

Ami

amibar avatar Apr 25 '24 11:04 amibar