Skip to content

Instantly share code, notes, and snippets.

@chuongmep
Created May 27, 2022
Embed
What would you like to do?
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