-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.cs
More file actions
82 lines (66 loc) · 2.31 KB
/
Copy pathDebug.cs
File metadata and controls
82 lines (66 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using CSharpAlgorithms.Audio;
namespace CSharpAlgorithms;
public static class Debug
{
public static void Print<TKey, TValue>(Dictionary<TKey, TValue> dictionary) where TKey : notnull
{
Console.WriteLine("{0,-15} {1,5}", "Key", "Value");
Console.WriteLine(new string('-', 22));
foreach (var kvp in dictionary)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
public static void PrintObject<T>(T obj)
{
Type type = typeof(T);
var fields = type.GetFields();
var properties = type.GetProperties();
foreach (var field in fields)
{
var value = field.GetValue(obj);
Console.WriteLine($"{field.Name}: {value}");
}
foreach (var prop in properties)
{
var value = prop.GetValue(obj);
Console.WriteLine($"{prop.Name}: {value}");
}
}
public static void Print(Exception ex)
{
Console.WriteLine($"Exception Type: {ex.GetType()}");
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine($"Source: {ex.Source}");
Console.WriteLine($"StackTrace: {ex.StackTrace}");
Console.WriteLine($"TargetSite: {ex.TargetSite}");
if (ex.InnerException != null)
{
Console.WriteLine("Inner Exception:");
Print(ex.InnerException);
}
}
public static void Print<T>(T[] array)
{
string message = "";
message += "[\n";
for (int i = 0; i < array.Length; i++)
{
string elementMessage = $"{i}: {typeof(T)}, {array[i]}\n";
message += $" {elementMessage}";
}
message += "]";
Console.WriteLine(message);
}
public static void WriteLine(int number, ConsoleColor colour = ConsoleColor.White) => WriteLine(number.ToString(), colour);
public static void WriteErrorLine(string message) => WriteLine(message, ConsoleColor.Red);
public static void WriteLine(string message, ConsoleColor colour = ConsoleColor.White)
{
Console.ForegroundColor = colour;
Console.WriteLine(message);
Console.ResetColor();
}
public static void WriteWarning(string message) => WriteLine(message, ConsoleColor.Yellow);
}