public static PbMessage CreateMessage(Type messageType, string name, object parent) { PbMessage _message = new PbMessage() { Messages = new ObservableCollection <PbMessage>(), Properties = new ObservableCollection <PbProperty>(), Name = name, Source = null, Parent = parent }; try { if (!messageType.IsGenericType) { foreach (PropertyInfo propertyInfo in messageType.GetProperties()) { string propertyName = string.Empty; if (propertyInfo.Name.StartsWith("Has") && propertyInfo.PropertyType.Equals(typeof(bool))) { propertyName = propertyInfo.Name.Substring(3); } else if (propertyInfo.Name.EndsWith("Count") && propertyInfo.PropertyType.Equals(typeof(int))) { propertyName = propertyInfo.Name.Substring(0, propertyInfo.Name.Length - 5) + "List"; } else { continue; } PropertyInfo _propertyInfo = messageType.GetProperty(propertyName); if (_propertyInfo.PropertyType.IsValueType || _propertyInfo.PropertyType.Equals(typeof(string))) { PbProperty pbProperty = new PbProperty() { Type = _propertyInfo.PropertyType, Name = _propertyInfo.Name, Value = "" }; _message.Properties.Add(pbProperty); } else { PbMessage pbMessage = GetMessage(_propertyInfo.PropertyType, _propertyInfo.Name, _message); _message.Messages.Add(pbMessage); } } } } catch (Exception ex) { logger.Error(ex); } return(_message); }
private static PbMessage GetMessage(object message, string name, object parent) { PbMessage _message = new PbMessage() { Messages = new ObservableCollection <PbMessage>(), Properties = new ObservableCollection <PbProperty>(), Name = name, Source = message, Parent = parent }; try { Type messageType = message.GetType(); if (messageType.IsGenericType) { int count = Convert.ToInt32(messageType.GetProperty("Count").GetValue(message, null)); string propertyName = name.Substring(0, name.Length - 4); for (int i = 0; i < count; i++) { PropertyInfo _propertyInfo = messageType.GetProperty("Item"); object value = _propertyInfo.GetValue(message, new object[] { i }); if (_propertyInfo.PropertyType.IsValueType || _propertyInfo.PropertyType.Equals(typeof(string))) { PbMessage pbMessage = new PbMessage() { Messages = new ObservableCollection <PbMessage>(), Properties = new ObservableCollection <PbProperty>() { new PbProperty() { Type = _propertyInfo.PropertyType, Name = propertyName, Value = value } }, Name = propertyName, Source = value, Parent = _message }; _message.Messages.Add(pbMessage); } else { PbMessage pbMessage = GetMessage(value, propertyName, _message); _message.Messages.Add(pbMessage); } } } else { foreach (PropertyInfo propertyInfo in messageType.GetProperties()) { string propertyName = string.Empty; if (propertyInfo.Name.StartsWith("Has") && propertyInfo.PropertyType.Equals(typeof(bool))) { propertyName = propertyInfo.Name.Substring(3); } else if (propertyInfo.Name.EndsWith("Count") && propertyInfo.PropertyType.Equals(typeof(int))) { propertyName = propertyInfo.Name.Substring(0, propertyInfo.Name.Length - 5) + "List"; } else { continue; } PropertyInfo _propertyInfo = messageType.GetProperty(propertyName); if (_propertyInfo.PropertyType.IsValueType || _propertyInfo.PropertyType.Equals(typeof(string))) { PbProperty pbProperty = new PbProperty() { Type = _propertyInfo.PropertyType, Name = _propertyInfo.Name, Value = _propertyInfo.GetValue(message, null) }; if (_propertyInfo.PropertyType.IsEnum) { pbProperty.Enum = new List <EnumEntity>(); foreach (var value in Enum.GetValues(_propertyInfo.PropertyType)) { pbProperty.Enum.Add(new EnumEntity() { Name = value.ToString() }); } } _message.Properties.Add(pbProperty); } else { PbMessage pbMessage = GetMessage(_propertyInfo.GetValue(message, null), _propertyInfo.Name, _message); _message.Messages.Add(pbMessage); } } } } catch (Exception ex) { logger.Error(ex); } return(_message); }