NQualityOfLife.Utils.LogParamExtensions.AsParam - Niilo007/RimWorld-NQoL GitHub Wiki

AsParam

Related:


C#

public static string AsParam(this float t, string? paramName = null)
{

}
public static string AsParam(this float? t, string? paramName = null)
{

}
internal static string AsParam<T>(this T? t, string? paramName = null)
{

}

Usage

The AsParam methods are most commonly used on error messages to give all the input parameters and their values:

public static byte SetOrAdd<K, V>([Mutated] this IDictionary<K, V> dictionary, K key, V value)
{
    string MyParams() => $"({dictionary.AsParam(nameof(dictionary))}, {key.AsParam(nameof(key))}, {value.AsParam(nameof(value))})";
    if (dictionary is null || key is null) { _ = Utils.Log.Error_Null(nameof(dictionary), nameof(key), method: MyParams()); return 0; }
    if (dictionary.ContainsKey(key))
    {
        dictionary[key] = value;
        return 1;
    }
    else
    {
        dictionary.Add(key, value);
        return 2;
    }
}

Errors


Examples

Used In Example:

private UnOrderedList<Line> _drawnLines = [];

//...

NQualityOfLife.Utils.Common.AddCustomFormatterForType<System.Windows.Shapes.Line>(formatter: (object? o, NQualityOfLife.Utils.Common.DisplayModeFlags flags) => {
    if (o is System.Windows.Shapes.Line l)
    {
        return $"{nameof(System.Windows.Shapes.Line)}{{({l.X1.RoundToInt()}, {l.Y1.RoundToInt()})<->({l.X2.RoundToInt()}, {l.Y2.RoundToInt()})}}";
    }
    throw new NotImplementedException("Passed object was not of the expected type!");
});

//...

DebugText.Text = _drawnLines.AsParam(nameof(_drawnLines));

Output from _drawnLines.AsParam(nameof(_drawnLines)):

UnOrderedList(5){Line{(50, 50)<->(227, 227)}, Line{(227, 227)<->(398, 122)}, Line{(0, 0)<->(796, 244)}, Line{(796, 244)<->(796, 0)}, Line{(796, 0)<->(0, 0)}} _drawnLines

If we don't add the formatter using AddCustomFormatterForType, we get this output:

UnOrderedList(5){System.Windows.Shapes.Line, System.Windows.Shapes.Line, System.Windows.Shapes.Line, System.Windows.Shapes.Line, System.Windows.Shapes.Line} _drawnLines


⚠️ **GitHub.com Fallback** ⚠️