Created
August 9, 2020 16:22
-
-
Save akvgergo/f6364035d15de21cf68aff90f37aa8de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Reflection; | |
using Newtonsoft.Json; | |
namespace Sandbox | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
StreamWriter writer = new StreamWriter("test.txt"); | |
Console.WriteLine(Inspector.ReadStatic(writer)); | |
Console.ReadKey(); | |
} | |
static string DebugSerialize(object obj) | |
{ | |
Dictionary<string, string> members = new Dictionary<string, string>(); | |
Type t = obj.GetType(); | |
foreach (var propInfo in t.GetProperties()) | |
{ | |
if (propInfo.CanRead) | |
{ | |
members[propInfo.Name] = propInfo.GetValue(obj).ToString(); | |
} | |
} | |
StringBuilder sb = new StringBuilder(); | |
sb.Append("{"); | |
foreach (var item in members) | |
{ | |
sb.Append($"\"{item.Key}\":{item.Value},\n"); | |
} | |
sb.Append("}"); | |
return sb.ToString(); | |
} | |
} | |
/// <summary> | |
/// Allows for printing extended runtime information about any object, in JSON or a JSON-esque format. | |
/// </summary> | |
public static class Inspector | |
{ | |
/// <summary> | |
/// whether to cache object maps created with reflection at runtime. May improve performance. | |
/// </summary> | |
public static bool CacheReflection { get; set; } = true; | |
/// <summary> | |
/// Cache for storing assembly data. | |
/// </summary> | |
static Dictionary<Type, TypeInfo> typeCache = new Dictionary<Type, TypeInfo>(); | |
/// <summary> | |
/// Reads all the static members of the given type. | |
/// </summary> | |
/// <param name="t">The type to inspect.</param> | |
/// <param name="options">The options to perform the read with.</param> | |
/// <returns>A <see cref="string"/> that contains all the information acquired.</returns> | |
static string ReadStatic(Type t, Options options) | |
{ | |
return null; | |
} | |
/// <summary> | |
/// Reads all the class members of the given <see cref="object"/>. | |
/// </summary> | |
/// <param name="obj">The instance to inspect.</param> | |
/// <param name="options">The options to perform the read with.</param> | |
/// <returns>A <see cref="string"/> that contains all the information acquired.</returns> | |
static string Read(object obj, Options options) | |
{ | |
return null; | |
} | |
public struct Options | |
{ | |
public int MaxRecursion { get; private set; } | |
public int ArrayLimiter { get; private set; } | |
public FormatOptions FormatOptions { get; private set; } | |
public ReadOptions ReadOptions { get; private set; } | |
public Options(FormatOptions Foptions = FormatOptions.All, ReadOptions Roptions = ReadOptions.AllPublic, int arrayCutoff = 16, int maxRecursion = -1) | |
{ | |
FormatOptions = Foptions; | |
ReadOptions = Roptions; | |
ArrayLimiter = arrayCutoff; | |
MaxRecursion = maxRecursion; | |
} | |
public static readonly Options Debug = new Options(FormatOptions.ShowErrors | FormatOptions.LimitedArrayStillDisplay | FormatOptions.ShortValueTypes, ReadOptions.AllPublic); | |
public static readonly Options ExtendedDebug = new Options(FormatOptions.PrependAccessModifier | FormatOptions.ShowErrors | FormatOptions.LimitedArrayStillDisplay, ReadOptions.AllInstanceMembers); | |
public static readonly Options BaseMethods = new Options(FormatOptions.ShowErrors | FormatOptions.ShortValueTypes, ReadOptions.BaseMethods); | |
public static readonly Options Methods = new Options(FormatOptions.PrependAccessModifier | FormatOptions.ShowErrors | FormatOptions.LimitedArrayStillDisplay | FormatOptions.ShortValueTypes, ReadOptions.AllSimpleMethods); | |
public static readonly Options AssemblyInfo = new Options(FormatOptions.All, ReadOptions.AllAssembly); | |
public static readonly Options FullLog = new Options(FormatOptions.All, ReadOptions.All); | |
} | |
/// <summary> | |
/// Defines how the object info should be serialized into a string | |
/// </summary> | |
[Flags] | |
public enum FormatOptions | |
{ | |
PrependSubtypeData = 1, | |
PrependAccessModifier = 2, | |
FullTypeNames = 4, | |
ShowErrors = 8, | |
ShowUnreadable = 16, | |
ShortValueTypes = 32, | |
HideAssemblyDataAfterFirstRecursion = 64, | |
LimitedArrayStillDisplay = 128, | |
All = int.MaxValue | |
} | |
/// <summary> | |
/// Defines what information should be serialized into the output. | |
/// </summary> | |
[Flags] | |
public enum ReadOptions { | |
//Instance info | |
PublicProperties = 1 << 0, | |
PrivateProperties = 1 << 1, | |
PublicFields = 1 << 2, | |
PrivateFields = 1 << 3, | |
PublicIndexedMembers = 1 << 4, | |
PrivateIndexedMembers = 1 << 5, | |
PublicSimpleMethods = 1 << 6, | |
PrivateSimpleMethods = 1 << 7, | |
BaseMethods = 1 << 8, | |
Events = 1 << 10, | |
//static info | |
StaticMembers = 1 << 9, | |
//poly info | |
TypeInfo = 1 << 11, | |
TypePolymorphism = 1 << 12, | |
//assembly info | |
Constants = 1 << 13, | |
DefinedOperators = 1 << 14, | |
NestedTypes = 1 << 15, | |
Attributes = 1 << 16, | |
Assembly = 1 << 17, | |
//Combo | |
AllPublic = PublicProperties | PublicFields | PublicIndexedMembers | PublicSimpleMethods, | |
AllPrivate = PrivateProperties | PrivateFields | PrivateIndexedMembers | PrivateSimpleMethods, | |
AllInstanceMembers = AllPublic | AllPrivate, | |
AllFields = PublicFields | PrivateFields, | |
AllProperties = PublicProperties | PrivateProperties, | |
AllSimpleMethods = PublicSimpleMethods | PrivateSimpleMethods, | |
AllStatic = StaticMembers | Events, | |
AllType = TypeInfo | TypePolymorphism, | |
AllAssembly = Constants | DefinedOperators | NestedTypes | Attributes | Assembly, | |
All = int.MaxValue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment