コード例 #1
0
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;

            if (service != null && value != null)
            {
                // ReSharper disable SuspiciousTypeConversion.Global
                var gridItem = provider as GridItem;
                // ReSharper restore SuspiciousTypeConversion.Global
                var propertyName = value.GetType().Name;
                if (gridItem != null)
                {
                    propertyName = gridItem.Label;
                }
                var label   = propertyName;
                var oldJson = JsonSerializerHelper.Serialize(value);
                using (var form = new CollectionEditorForm(string.Format("Edit {0}", propertyName), label, value))
                {
                    if (service.ShowDialog(form) == DialogResult.OK)
                    {
                        if (string.Compare(oldJson,
                                           JsonSerializerHelper.Serialize(value),
                                           StringComparison.InvariantCulture) != 0)
                        {
                            value = GenericCopier <object> .DeepCopy(form.Value);
                        }
                    }
                }
            }
            // ReSharper disable AssignNullToNotNullAttribute
            return(value);
            // ReSharper restore AssignNullToNotNullAttribute
        }
コード例 #2
0
 /// <summary>
 /// Writes the checkpoints for event hub partitions to a JSON file in the current directory.
 /// </summary>
 public static void WriteCheckpoints()
 {
     if (itemList.Count == 0)
     {
         return;
     }
     JsonSerializerHelper.Serialize(itemList, Formatting.Indented);
     WriteFile(filePath, JsonSerializerHelper.Serialize(itemList, Formatting.Indented));
 }
コード例 #3
0
        public IEnumerable <BrokeredMessage> GenerateBrokeredMessageCollection(int brokeredMessageCount, WriteToLogDelegate writeToLog)
        {
            if (brokeredMessageCount < 0)
            {
                return(null);
            }
            var random      = new Random();
            var messageList = new List <BrokeredMessage>();

            for (var i = 0; i < brokeredMessageCount; i++)
            {
                try
                {
                    var normalState = AlertState == (int)OnOff.Off ? OnOff.On : OnOff.Off;
                    var alertState  = AlertState == (int)OnOff.Off ? OnOff.Off : OnOff.On;
                    var payload     = new OnOffDeviceEvent
                    {
                        EventId   = EventId++,
                        DeviceId  = random.Next(MinDeviceId, MaxDeviceId + 1),
                        Value     = random.Next(1, 101) <= AlertPercentage ? alertState : normalState,
                        Timestamp = DateTime.UtcNow
                    };
                    var text = MessageFormat == MessageFormat.Json
                        ? JsonSerializerHelper.Serialize(payload)
                        : XmlSerializerHelper.Serialize(payload);
                    var brokeredMessage = new BrokeredMessage(text.ToMemoryStream())
                    {
                        MessageId = payload.DeviceId.ToString(CultureInfo.InvariantCulture),
                    };
                    brokeredMessage.Properties.Add("eventId", payload.EventId);
                    brokeredMessage.Properties.Add("deviceId", payload.DeviceId);
                    brokeredMessage.Properties.Add("value", (int)payload.Value);
                    brokeredMessage.Properties.Add("time", DateTime.UtcNow.Ticks);
                    brokeredMessage.Properties.Add("city", City);
                    brokeredMessage.Properties.Add("country", Country);
                    brokeredMessage.Label = Label;
                    messageList.Add(brokeredMessage);
                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrWhiteSpace(ex.Message))
                    {
                        writeToLog(string.Format(CultureInfo.CurrentCulture, ExceptionFormat, ex.Message));
                    }
                    if (ex.InnerException != null && !string.IsNullOrWhiteSpace(ex.InnerException.Message))
                    {
                        writeToLog(string.Format(CultureInfo.CurrentCulture, InnerExceptionFormat, ex.InnerException.Message));
                    }
                }
            }
            if (writeToLog != null)
            {
                writeToLog(string.Format(BrokeredMessageCreatedFormat, messageList.Count));
            }
            return(messageList);
        }
コード例 #4
0
        public static string Serialize(object entity, string body, bool doNotSerializeBody = false)
        {
            if (entity == null)
            {
                return(null);
            }
            var type = entity.GetType();

            GetProperties(type);
            if (!propertyCache.ContainsKey(type.FullName))
            {
                return(null);
            }
            var propertyDictionary = propertyCache[type.FullName];
            var entityDictionary   = new SortedDictionary <string, object>();

            if (!doNotSerializeBody && JsonSerializerHelper.IsJson(body))
            {
                try
                {
                    entityDictionary.Add("body", JObject.Parse(body));
                }
                catch (Exception)
                {
                    try
                    {
                        entityDictionary.Add("body", JArray.Parse(body));
                    }
                    catch (Exception)
                    {
                        entityDictionary.Add("body", body);
                    }
                }
            }
            else
            {
                entityDictionary.Add("body", body);
            }
            foreach (var keyValuePair in propertyDictionary)
            {
                var camelCase = string.Format("{0}{1}",
                                              keyValuePair.Key.Substring(0, 1).ToLower(CultureInfo.InvariantCulture),
                                              keyValuePair.Key.Substring(1, keyValuePair.Key.Length - 1));
                entityDictionary.Add(camelCase, null);
                try
                {
                    var value = keyValuePair.Value.GetValue(entity, null);
                    entityDictionary[camelCase] = value;
                }
                // ReSharper disable once EmptyGeneralCatchClause
                catch (Exception)
                {
                }
            }
            return(JsonSerializerHelper.Serialize(entityDictionary, Formatting.Indented));
        }
コード例 #5
0
        public IEnumerable <EventData> GenerateEventDataCollection(int eventDataCount, WriteToLogDelegate writeToLog)
        {
            if (eventDataCount < 0)
            {
                return(null);
            }
            var random = new Random();
            var list   = new List <EventData>();

            for (var i = 0; i < eventDataCount; i++)
            {
                try
                {
                    var payload = new ThresholdDeviceEvent
                    {
                        EventId   = EventId++,
                        DeviceId  = random.Next(MinDeviceId, MaxDeviceId + 1),
                        Value     = random.Next(MinDeviceId, MaxDeviceId + 1),
                        Timestamp = DateTime.UtcNow
                    };
                    var eventData = new EventData((MessageFormat == MessageFormat.Json
                        ? JsonSerializerHelper.Serialize(payload)
                        : XmlSerializerHelper.Serialize(payload)).ToMemoryStream())
                    {
                        PartitionKey = payload.DeviceId.ToString(CultureInfo.InvariantCulture),
                    };
                    eventData.Properties.Add("eventId", payload.EventId);
                    eventData.Properties.Add("deviceId", payload.DeviceId);
                    eventData.Properties.Add("value", payload.Value);
                    eventData.Properties.Add("time", DateTime.UtcNow.Ticks);
                    eventData.Properties.Add("city", City);
                    eventData.Properties.Add("country", Country);
                    list.Add(eventData);
                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrWhiteSpace(ex.Message))
                    {
                        writeToLog(string.Format(CultureInfo.CurrentCulture, ExceptionFormat, ex.Message));
                    }
                    if (ex.InnerException != null && !string.IsNullOrWhiteSpace(ex.InnerException.Message))
                    {
                        writeToLog(string.Format(CultureInfo.CurrentCulture, InnerExceptionFormat, ex.InnerException.Message));
                    }
                }
            }
            if (writeToLog != null)
            {
                writeToLog(string.Format(EventDataCreatedFormat, list.Count));
            }
            return(list);
        }
コード例 #6
0
        public static string Serialize(IEnumerable <object> entities, IEnumerable <string> bodies)
        {
            var entityEnumerable = entities as object[] ?? entities.ToArray();
            var bodyEnumerable   = bodies as string[] ?? bodies.ToArray();

            if (entityEnumerable.Length == 0 || bodyEnumerable.Length == 0 ||
                entityEnumerable.Length != bodyEnumerable.Length)
            {
                return(null);
            }
            var type = entityEnumerable[0].GetType();

            GetProperties(type);
            if (!propertyCache.ContainsKey(type.FullName))
            {
                return(null);
            }
            var propertyDictionary = propertyCache[type.FullName];
            var entityList         = new List <SortedDictionary <string, object> >(entityEnumerable.Length);

            for (var i = 0; i < entityEnumerable.Length; i++)
            {
                var entityDictionary = new SortedDictionary <string, object>();
                if (JsonSerializerHelper.IsJson(bodyEnumerable[i]))
                {
                    entityDictionary.Add("body", JObject.Parse(bodyEnumerable[i]));
                }
                else
                {
                    entityDictionary.Add("body", bodyEnumerable[i]);
                }
                foreach (var keyValuePair in propertyDictionary)
                {
                    var camelCase = string.Format("{0}{1}",
                                                  keyValuePair.Key.Substring(0, 1).ToLower(CultureInfo.InvariantCulture),
                                                  keyValuePair.Key.Substring(1, keyValuePair.Key.Length - 1));
                    entityDictionary.Add(camelCase, null);
                    try
                    {
                        var value = keyValuePair.Value.GetValue(entityEnumerable[i], null);
                        entityDictionary[camelCase] = value;
                    }
                    // ReSharper disable once EmptyGeneralCatchClause
                    catch (Exception)
                    {
                    }
                }
                entityList.Add(entityDictionary);
            }
            return(JsonSerializerHelper.Serialize(entityList.ToArray(), Formatting.Indented));
        }