Skip to content

Instantly share code, notes, and snippets.

@chuongmep
Created May 27, 2022 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuongmep/227be2a7ee65cb8b8095bae25ab16fd3 to your computer and use it in GitHub Desktop.
Save chuongmep/227be2a7ee65cb8b8095bae25ab16fd3 to your computer and use it in GitHub Desktop.
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