public static object SetPublicProperties(this object instance, PropertyWithValue[] props) { var propertyInfos = instance.GetType().GetProperties(); foreach (var propertyInfo in propertyInfos) { var info = propertyInfo; foreach (var prop in props.Where(prop => info.Name.Equals(prop.Name))) { var convertedValue = Convert.ChangeType(prop.Value, propertyInfo.PropertyType); propertyInfo.SetValue(instance, convertedValue); } } return instance; }
private void SendMessageWithBus(Type messageType, PropertyWithValue[] propertyValues = null) { _customLogger.Info("Sending message: " + messageType.Name + " using service bus."); var message = _safeExecutionHelper.ExecuteSafely<object, TypeLoadException>(() => { var instance = Activator.CreateInstance(messageType); if (propertyValues != null) { return instance.SetPublicProperties(propertyValues); } return instance; }); _messageBusManager.SendMessage(message); _customLogger.Info("Message sent successfully!"); }