Пример #1
0
 /// <summary>
 /// write data and convert to json for serialize
 /// </summary>
 /// <param name="baseType"></param>
 /// <param name="instance"></param>
 /// <param name="writer"></param>
 /// <param name="serializer"></param>
 void WriteData(Type baseType, object instance, JsonWriter writer, JsonSerializer serializer)
 {
     try
     {
         foreach (var property in baseType.GetProperties())
         {
             if (property.CanRead)
             {
                 var  implementICollection = (SkipDataExchangeAttribute)property.GetCustomAttributes(typeof(SkipDataExchangeAttribute), true).FirstOrDefault();
                 var  canIgnore            = implementICollection == null ? (bool?)null : implementICollection.CanIgnore(instance, property, baseType, implementICollection);
                 bool isIgnored            = false;
                 if (canIgnore.HasValue)
                 {
                     if (canIgnore.Value)
                     {
                         isIgnored = true;
                     }
                 }
                 else if (implementICollection != null && (implementICollection.Mode == LimitExchangeType.Both || implementICollection.Mode == Mode))
                 {
                     isIgnored = true;
                 }
                 if (!isIgnored)
                 {
                     if (ExchangerTypes != null)
                     {
                         var find = ExchangerTypes.FirstOrDefault(x => x.Type == baseType && (x.LimitationMode == LimitExchangeType.Both || x.LimitationMode == LimitExchangeType.OutgoingCall));
                         if (find != null)
                         {
                             if (find.CustomDataExchangerType == CustomDataExchangerType.Take)
                             {
                                 if (find.Properties != null && !find.Properties.Contains(property.Name) && find.IsEnabled(Client, Server, property.Name, baseType))
                                 {
                                     isIgnored = true;
                                 }
                             }
                             else
                             {
                                 if (find.Properties != null && find.Properties.Contains(property.Name) && find.IsEnabled(Client, Server, property.Name, baseType))
                                 {
                                     isIgnored = true;
                                 }
                             }
                         }
                     }
                 }
                 if (!isIgnored)
                 {
                     bool isPropertyArray      = typeof(IEnumerable).IsAssignableFrom(property.PropertyType) && property.PropertyType != typeof(string);
                     bool isPropertyDictionary = typeof(IDictionary).IsAssignableFrom(property.PropertyType);
                     if (isPropertyArray || isPropertyDictionary)
                     {
                         object propValue = null;
                         try
                         {
                             propValue = property.GetValue(instance, null);
                         }
                         catch (Exception ex)
                         {
                             AutoLogger.LogError(ex, "WriteData 1");
                         }
                         if (propValue != null)
                         {
                             try
                             {
                                 writer.WritePropertyName(property.Name);
                                 serializer.Serialize(writer, propValue);
                             }
                             catch (Exception ex)
                             {
                                 AutoLogger.LogError(ex, "WriteData 2");
                             }
                         }
                     }
                     else
                     {
                         try
                         {
                             var value = property.GetValue(instance, null);
                             writer.WritePropertyName(property.Name);
                             serializer.Serialize(writer, value);
                         }
                         catch (Exception ex)
                         {
                             AutoLogger.LogError(ex, "WriteData 3");
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         AutoLogger.LogError(ex, "WriteData 4");
     }
 }
Пример #2
0
        /// <summary>
        /// generate properties of object for deserialze
        /// </summary>
        /// <param name="instance"></param>
        void GenerateProperties(object instance)
        {
            if (instance == null)
            {
                return;
            }
            var type = instance.GetType();

            if (SerializeHelper.GetTypeCodeOfObject(instance.GetType()) != SerializeObjectType.Object)
            {
                return;
            }
            foreach (var property in type.GetProperties())
            {
                if (property.CanRead)
                {
                    var  implementICollection = (SkipDataExchangeAttribute)property.GetCustomAttributes(typeof(SkipDataExchangeAttribute), true).FirstOrDefault();
                    var  canIgnore            = implementICollection == null ? (bool?)null : implementICollection.CanIgnore(instance, property, type, implementICollection);
                    bool isIgnored            = false;
                    if (canIgnore.HasValue)
                    {
                        if (canIgnore.Value)
                        {
                            isIgnored = true;
                        }
                    }
                    else if (implementICollection != null && (implementICollection.Mode == LimitExchangeType.Both || implementICollection.Mode == Mode))
                    {
                        isIgnored = true;
                    }
                    if (!isIgnored)
                    {
                        if (ExchangerTypes != null)
                        {
                            var find = ExchangerTypes.FirstOrDefault(x => x.Type == type && (x.LimitationMode == LimitExchangeType.Both || x.LimitationMode == LimitExchangeType.IncomingCall));
                            if (find != null)
                            {
                                if (find.CustomDataExchangerType == CustomDataExchangerType.Take)
                                {
                                    if (find.Properties != null && !find.Properties.Contains(property.Name) && find.IsEnabled(Client, Server, property.Name, type))
                                    {
                                        isIgnored = true;
                                    }
                                }
                                else
                                {
                                    if (find.Properties != null && find.Properties.Contains(property.Name) && find.IsEnabled(Client, Server, property.Name, type))
                                    {
                                        isIgnored = true;
                                    }
                                }
                            }
                        }
                    }
                    if (isIgnored)
                    {
                        property.SetValue(instance, null, null);
                    }
                    else
                    {
                        bool isPropertyArray      = typeof(IEnumerable).IsAssignableFrom(property.PropertyType) && property.PropertyType != typeof(string);
                        bool isPropertyDictionary = typeof(IDictionary).IsAssignableFrom(property.PropertyType);
                        if (isPropertyDictionary)
                        {
                            var value = property.GetValue(instance, null);
                            if (value != null)
                            {
                                foreach (DictionaryEntry item in (IDictionary)value)
                                {
                                    GenerateProperties(item.Key);
                                    GenerateProperties(item.Value);
                                }
                            }
                        }
                        else if (isPropertyArray)
                        {
                            var value = property.GetValue(instance, null);
                            if (value != null)
                            {
                                foreach (object item in (IEnumerable)value)
                                {
                                    GenerateProperties(item);
                                }
                            }
                        }
                    }
                }
            }
        }