public static JSONField[] GetJSONFields(Type objType) { //Find all fields in type that have been marked with the JSONFieldAttribute List <JSONField> JSONFields = new List <JSONField>(); BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy; //First find fields FieldInfo[] fields = objType.GetFields(bindingAttr); foreach (FieldInfo field in fields) { JSONFieldAttribute fieldAttribute = SystemUtils.GetAttribute <JSONFieldAttribute>(field); if (fieldAttribute != null) { JSONField JSONField = new JSONField(fieldAttribute, field); JSONFields.Add(JSONField); } } //Then find all properties PropertyInfo[] properties = objType.GetProperties(bindingAttr); foreach (PropertyInfo property in properties) { JSONFieldAttribute fieldAttribute = SystemUtils.GetAttribute <JSONFieldAttribute>(property); if (fieldAttribute != null) { JSONField JSONField = new JSONField(fieldAttribute, property); JSONFields.Add(JSONField); } } return(JSONFields.ToArray()); }
public JSONField(JSONFieldAttribute attribute, PropertyInfo propertyInfo) { _attribute = attribute; _fieldInfo = null; _propertyInfo = propertyInfo; _id = GetId(attribute, propertyInfo); }
private static string GetId(JSONFieldAttribute attribute, MemberInfo info) { if (string.IsNullOrEmpty(attribute.ID)) { string id = info.Name; return id.TrimStart('_'); } else return attribute.ID; }