Created
May 27, 2022 02:56
-
-
Save chuongmep/227be2a7ee65cb8b8095bae25ab16fd3 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
public static Dictionary<string, string> SnoopProperties(this object obj, bool isTraceDebug) | |
{ | |
Dictionary<string, string> data = new Dictionary<string, string>(); | |
Type type = obj.GetType(); | |
foreach (PropertyInfo propertyInfo in type.GetProperties()) | |
{ | |
try | |
{ | |
object value = propertyInfo.GetValue(obj); | |
if (!data.ContainsKey(propertyInfo.Name)) | |
{ | |
data.Add(propertyInfo.Name, value.ToString()); | |
} | |
} | |
catch (Exception e) | |
{ | |
if (!data.ContainsKey(propertyInfo.Name)) | |
{ | |
data.Add(propertyInfo.Name, e.Message); | |
} | |
} | |
} | |
if (isTraceDebug) | |
{ | |
foreach (KeyValuePair<string, string> s in data) | |
{ | |
Debug.WriteLine($"{s.Key} : {s.Value}"); | |
} | |
} | |
return data; | |
} | |
public static Dictionary<string, string> SnoopMethod(this object obj, bool isDebug) | |
{ | |
Dictionary<string, string> data = new Dictionary<string, string>(); | |
Type type = obj.GetType(); | |
foreach (MethodInfo methodInfo in type.GetMethods()) | |
{ | |
if (methodInfo.Name.StartsWith("get_") || methodInfo.Name.StartsWith("set_")) | |
{ | |
continue; | |
} | |
if (!data.ContainsKey(methodInfo.Name)) | |
{ | |
try | |
{ | |
if (methodInfo.IsStatic) | |
{ | |
methodInfo.Invoke(null, null); | |
} | |
else | |
{ | |
data.Add(methodInfo.Name, methodInfo.Invoke(obj, null).ToString()); | |
} | |
} | |
catch (Exception e) | |
{ | |
data.Add(methodInfo.Name, e.Message); | |
} | |
} | |
} | |
if (isDebug) | |
{ | |
foreach (KeyValuePair<string, string> s in data) | |
{ | |
Debug.WriteLine($"{s.Key} : {s.Value}"); | |
} | |
} | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment