/// <summary> /// Uses reflection to recursively generate UIDisplayData object /// If UIAttribute.DisplayName == "", the UIField DisplayName will be generated by splitting the property name at capital letters (assuming we use camel case) /// A property/field marked with the attribute [UIProperty] will be added UIDisplayData.UIFields /// A property/field marked with the attribute [UICollection] will be recursively processed and added to UIDisplayData.NestedUIDisplayData /// </summary> /// <param name="obj"></param> /// <returns></returns> static public UIDisplayData GetUIData(IHasUIData obj) { var displayData = new UIDisplayData { DisplayName = obj.UIDisplayName }; PopulateUIData(obj, ref displayData); return(displayData); }
/// <summary> /// Uses reflection to generate (displayName, displayData) key value pairs /// the returned dictionary always contains a <string, string> ("ObjectType", [ObjectType].ToString()) to identify the serialized object type /// If UIAttribute.DisplayName == "", the UIField DisplayName will be generated by splitting the property name at capital letters (assuming we use camel case) /// A property/field marked with the attribute [UIProperty] will be added to the dictionary as a kvp<string, UIField>(DisplayName, UIField) /// A property/field marked with the attribute [UICollection] will be added to the dictionary as a kvp<string, UIGroup>(DisplayName, UIGroup) /// </summary> /// <param name="obj"></param> /// <returns></returns> public static List <KeyValuePair <string, object> > GetUIData(IHasUIData obj) { var UIData = new List <KeyValuePair <string, object> >(); UIData.Add(new KeyValuePair <string, object>("ObjectType", obj.GetType().Name)); GetUIFields(obj, ref UIData); GetUIGroups(obj, ref UIData); return(UIData); }
/// <summary> /// Get UI data from complex (class) members marked with UICollection /// </summary> /// <param name="obj"></param> /// <param name="UIData"></param> static void GetUICollections(IHasUIData obj, ref UIDisplayData UIData) { foreach (var prop in GetUICollectionProperties(obj)) { var uiattribute = prop.GetCustomAttribute(typeof(UICollection)) as UICollection; var propertyName = prop.Name; var displayName = uiattribute.DisplayName == "" ? prop.Name.SplitCamelCase() : uiattribute.DisplayName; var nestedData = new UIDisplayData() { DisplayName = displayName }; if (prop.PropertyType.GetInterface(typeof(IHasUIData).Name) != null) { nestedData.ObjectType = prop.PropertyType.Name; PopulateUIData((IHasUIData)prop.GetValue(obj), ref nestedData); UIData.NestedUIDisplayData.Add(propertyName, nestedData); } else if (prop.PropertyType.GetInterface(typeof(IEnumerable).Name) != null) { nestedData.ObjectType = GetFullType(prop.PropertyType); PopulateUIData((IEnumerable)prop.GetValue(obj), ref nestedData); UIData.NestedUIDisplayData.Add(propertyName, nestedData); } } foreach (var field in GetUICollectionFields(obj)) { var uiattribute = field.GetCustomAttribute(typeof(UICollection)) as UICollection; var fieldName = field.Name; var displayName = uiattribute.DisplayName == "" ? field.Name.SplitCamelCase() : uiattribute.DisplayName; var nestedData = new UIDisplayData() { DisplayName = displayName }; if (field.FieldType.GetInterface(typeof(IHasUIData).Name) != null) { nestedData.ObjectType = field.FieldType.Name; PopulateUIData((IHasUIData)field.GetValue(obj), ref nestedData); UIData.NestedUIDisplayData.Add(fieldName, nestedData); } else if (field.FieldType.GetInterface(typeof(IEnumerable).Name) != null) { nestedData.ObjectType = GetFullType(field.FieldType); PopulateUIData((IEnumerable)field.GetValue(obj), ref nestedData); UIData.NestedUIDisplayData.Add(fieldName, nestedData); } } }
static IEnumerable <PropertyInfo> GetUICollectionProperties(IHasUIData obj) { var t = obj.GetType(); if (_UICollectionPropertyCache.ContainsKey(t)) { return(_UICollectionPropertyCache[t]); } else { var properties = t.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where( prop => Attribute.IsDefined(prop, typeof(UICollection))); _UICollectionPropertyCache.TryAdd(t, properties); return(properties); } }
static IEnumerable <FieldInfo> GetUICollectionFields(IHasUIData obj) { var t = obj.GetType(); if (_UICollectionFieldCache.ContainsKey(t)) { return(_UICollectionFieldCache[t]); } else { var fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where( field => Attribute.IsDefined(field, typeof(UICollection))); _UICollectionFieldCache.TryAdd(t, fields); return(fields); } }
private static void GetUIGroups(IHasUIData obj, ref List <KeyValuePair <string, object> > UIData) { var properties = obj.GetType() .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(prop => Attribute.IsDefined(prop, typeof(UICollection))); var fields = obj.GetType() .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(field => Attribute.IsDefined(field, typeof(UICollection))); foreach (var prop in properties) { var uiattribute = prop.GetCustomAttribute(typeof(UICollection)) as UICollection; if (prop.PropertyType.GetInterface(typeof(IHasUIData).Name) == null) { continue; } var displayName = uiattribute.DisplayName == "" ? prop.Name.SplitCamelCase() : uiattribute.DisplayName; var uifields = new List <KeyValuePair <string, object> >(); GetUIFields((IHasUIData)prop.GetValue(obj), ref uifields); UIData.Add(new KeyValuePair <string, object>(displayName, new UIGroup(uifields))); } foreach (var field in fields) { var uiattribute = field.GetCustomAttribute(typeof(UICollection)) as UICollection; if (field.FieldType.GetInterface(typeof(IHasUIData).Name) == null) { continue; } var displayName = uiattribute.DisplayName == "" ? field.Name.SplitCamelCase() : uiattribute.DisplayName; var uifields = new List <KeyValuePair <string, object> >(); GetUIFields((IHasUIData)field.GetValue(obj), ref uifields); UIData.Add(new KeyValuePair <string, object>(displayName, new UIGroup(uifields))); } }
static void GetUIFields(IHasUIData obj, ref List <KeyValuePair <string, object> > UIData) { var properties = obj.GetType() .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(prop => Attribute.IsDefined(prop, typeof(UIProperty))); var fields = obj.GetType() .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(field => Attribute.IsDefined(field, typeof(UIProperty))); foreach (var prop in properties) { var uiattribute = prop.GetCustomAttribute(typeof(UIProperty)) as UIProperty; var displayName = uiattribute.DisplayName == "" ? prop.Name.SplitCamelCase() : uiattribute.DisplayName; var displayValue = prop.GetValue(obj).ToString(); var displayOrder = uiattribute.DisplayOrder; var units = uiattribute.Units; var isDisplayed = uiattribute.IsDisplayed; UIData.Add(new KeyValuePair <string, object>(displayName, new UIField { DisplayValue = displayValue, DisplayOrder = displayOrder, Units = units, IsDisplayed = isDisplayed })); } foreach (var field in fields) { var uiattribute = field.GetCustomAttribute(typeof(UIProperty)) as UIProperty; var displayName = uiattribute.DisplayName == "" ? field.Name.SplitCamelCase() : uiattribute.DisplayName; var displayValue = field.GetValue(obj).ToString(); var displayOrder = uiattribute.DisplayOrder; var units = uiattribute.Units; var isDisplayed = uiattribute.IsDisplayed; UIData.Add(new KeyValuePair <string, object>(displayName, new UIField { DisplayValue = displayValue, DisplayOrder = displayOrder, Units = units, IsDisplayed = isDisplayed })); } }
/// <summary> /// Get UI data from simple (non-class) members marked with UIProperty /// </summary> /// <param name="obj"></param> /// <param name="UIData"></param> static void GetUIFields(IHasUIData obj, ref UIDisplayData UIData) { foreach (var prop in GetUIPropertyProperties(obj)) { var uiattribute = prop.GetCustomAttribute(typeof(UIProperty)) as UIProperty; var propertyName = prop.Name; var displayName = uiattribute.DisplayName == "" ? prop.Name.SplitCamelCase() : uiattribute.DisplayName; var displayValue = prop.GetValue(obj).ToString(); var displayOrder = uiattribute.DisplayOrder; var units = uiattribute.Units; var isDisplayed = uiattribute.IsDisplayed; var type = prop.PropertyType.Name; UIData.UIFields.Add(propertyName, new UIField { DisplayName = displayName, DisplayValue = displayValue, DisplayOrder = displayOrder, Units = units, IsDisplayed = isDisplayed, Type = type }); } foreach (var field in GetUIPropertyFields(obj)) { var uiattribute = field.GetCustomAttribute(typeof(UIProperty)) as UIProperty; var propertyName = field.Name; var displayName = uiattribute.DisplayName == "" ? field.Name.SplitCamelCase() : uiattribute.DisplayName; var displayValue = field.GetValue(obj).ToString(); var displayOrder = uiattribute.DisplayOrder; var units = uiattribute.Units; var isDisplayed = uiattribute.IsDisplayed; var type = field.FieldType.Name; UIData.UIFields.Add(propertyName, new UIField { DisplayName = displayName, DisplayValue = displayValue, DisplayOrder = displayOrder, Units = units, IsDisplayed = isDisplayed, Type = type }); } }
/// <summary> /// If displayName == null, DisplayName is set to obj.UIDisplayName /// </summary> /// <param name="obj"></param> /// <param name="UIData"></param> /// <param name="displayName"></param> static void PopulateUIData(IHasUIData obj, ref UIDisplayData UIData) { UIData.ObjectType = obj.GetType().Name; GetUIFields(obj, ref UIData); GetUICollections(obj, ref UIData); }