Last active
July 15, 2017 10:05
-
-
Save zhenlinyang/fae29d09f6ca7cc7c95020f0c2b58ecd to your computer and use it in GitHub Desktop.
Object Status
This file contains hidden or 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.Generic; | |
using System.Reflection; | |
using System.Text; | |
public static class StatusHandler | |
{ | |
private static readonly Dictionary<string, BindingFlags> m_BindingFlagsDic = | |
new Dictionary<string, BindingFlags> | |
{ | |
{"静私", BindingFlags.Static | BindingFlags.NonPublic}, | |
{"静公", BindingFlags.Static | BindingFlags.Public}, | |
{"实私", BindingFlags.Instance | BindingFlags.NonPublic}, | |
{"实公", BindingFlags.Instance | BindingFlags.Public}, | |
}; | |
public static string Status(this object obj, string objName = null) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
Type type = obj.GetType(); | |
sb.AppendLine(null == objName ? "对象类型=" + type : objName + "类型=" + type); | |
foreach (var pair in m_BindingFlagsDic) | |
{ | |
var fields = type.GetFields(pair.Value); | |
var properties = type.GetProperties(pair.Value); | |
foreach (var e in fields) | |
{ | |
var str = string.Format("[{0}] {1} {2}={3}\t宣={4}\t反={5}", pair.Key + "属", e.FieldType, e.Name, | |
e.GetValue(obj), e.DeclaringType, e.ReflectedType); | |
sb.AppendLine(str); | |
} | |
foreach (var e in properties) | |
{ | |
var str = string.Format("[{0}] {1} {2}={3}\t宣={4}\t反={5}", pair.Key + "字", e.PropertyType, e.Name, | |
e.GetValue(obj, null), e.DeclaringType, e.ReflectedType); | |
sb.AppendLine(str); | |
} | |
} | |
sb.AppendLine(); | |
return sb.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment