コード例 #1
0
        private static void PropertySetValue(object instanceObj, PropertyInfo currentPropertyInfo, object objvalue, IPropertyAccessorManager propertyAccessorManager)
        {
            Type   serializeObjectType      = instanceObj.GetType();
            string propertyKey              = serializeObjectType.FullName + "." + currentPropertyInfo.Name;
            PropertyInfoContainer container = new PropertyInfoContainer {
                PropertyName = currentPropertyInfo.Name, InstanceTypeHandle = serializeObjectType.TypeHandle, PropertyTypeHandle = currentPropertyInfo.PropertyType.TypeHandle
            };

            propertyAccessorManager.GetPropertyAccessor(propertyKey, container).SetValue(instanceObj, objvalue);
        }
コード例 #2
0
        /// <summary>
        /// 序列化成JSON字符串
        /// </summary>
        /// <param name="serializeObject"></param>
        /// <param name="setting"></param>
        /// <param name="isShowPropertyName"></param>
        /// <param name="valueSb"></param>
        /// <param name="propertyAccessorManager"></param>
        /// <param name="propertyName"></param>
        public static void Serialize(object serializeObject, SerializationSetting setting, bool isShowPropertyName, StringBuilder valueSb, IPropertyAccessorManager propertyAccessorManager, string propertyName = "")
        {
            if (serializeObject == null)
            {
                if (isShowPropertyName && !string.IsNullOrWhiteSpace(propertyName))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(propertyName);
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(JsonSymbol.JsonPropertySymbol);
                    valueSb.Append(JsonSymbol.JsonNullSymbol);
                }
                else
                {
                    valueSb.Append(JsonSymbol.JsonNullSymbol);
                }
                return;
            }
            Type serializeObjectType = serializeObject.GetType();

            if (typeof(IEnumerable).IsAssignableFrom(serializeObjectType) && serializeObjectType != typeof(String) && !typeof(IDictionary).IsAssignableFrom(serializeObjectType))
            {
                IEnumerable objectValue    = (IEnumerable)serializeObject;
                IEnumerator enumeratorList = objectValue.GetEnumerator();
                int         objCount       = enumeratorList.GetEnumeratorCount();
                enumeratorList.Reset();
                int tmpObjCount = 0;
                if (isShowPropertyName && !string.IsNullOrWhiteSpace(propertyName))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(propertyName);
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(JsonSymbol.JsonPropertySymbol);
                }
                valueSb.Append(JsonSymbol.JsonArraySymbol_Begin);
                while (enumeratorList.MoveNext())
                {
                    Type enumeratorCurrentType = enumeratorList.Current.GetType();
                    if (enumeratorCurrentType.IsNormalType())
                    {
                        Serialize(enumeratorList.Current, setting, false, valueSb, propertyAccessorManager);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(propertyName))
                        {
                            Serialize(enumeratorList.Current, setting, false, valueSb, propertyAccessorManager);
                        }
                        else
                        {
                            Serialize(enumeratorList.Current, setting, true, valueSb, propertyAccessorManager);
                        }
                    }
                    tmpObjCount++;
                    if (tmpObjCount < objCount)
                    {
                        valueSb.Append(JsonSymbol.JsonSeparateSymbol);
                    }
                }
                valueSb.Append(JsonSymbol.JsonArraySymbol_End);
            }
            else if (typeof(IDictionary).IsAssignableFrom(serializeObjectType))
            {
                IDictionary objectValue    = (IDictionary)serializeObject;
                int         objCount       = objectValue.Count;
                ICollection keyList        = objectValue.Keys;
                IEnumerator enumeratorList = keyList.GetEnumerator();
                int         tmpObjCount    = 0;
                if (isShowPropertyName && !string.IsNullOrWhiteSpace(propertyName))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(propertyName);
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(JsonSymbol.JsonPropertySymbol);
                }
                valueSb.Append(JsonSymbol.JsonObjectSymbol_Begin);
                while (enumeratorList.MoveNext())
                {
                    if (enumeratorList.Current == null)
                    {
                        throw new Exception("Key Is Not Null");
                    }
                    Type enumeratorCurrentType = objectValue[enumeratorList.Current].GetType();
                    Serialize(objectValue[enumeratorList.Current], setting, true, valueSb, propertyAccessorManager, enumeratorList.Current.ToString());
                    tmpObjCount++;
                    if (tmpObjCount < objCount)
                    {
                        valueSb.Append(JsonSymbol.JsonSeparateSymbol);
                    }
                }
                valueSb.Append(JsonSymbol.JsonObjectSymbol_End);
            }
            else if (serializeObjectType.IsNormalType())
            {
                Process(serializeObject, valueSb, setting, isShowPropertyName, propertyName);
            }
            else
            {
                if (isShowPropertyName && !string.IsNullOrWhiteSpace(propertyName))
                {
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(propertyName);
                    valueSb.Append(JsonSymbol.JsonQuotesSymbol);
                    valueSb.Append(JsonSymbol.JsonPropertySymbol);
                }
                valueSb.Append(JsonSymbol.JsonObjectSymbol_Begin);
                PropertyInfo[] propertyInfoList = serializeObject.GetType().GetProperties();
                int            propertyCount    = propertyInfoList.Length;
                int            propertyIndex    = 0;
                foreach (PropertyInfo propertyInfoItem in propertyInfoList)
                {
                    string propertyKey = serializeObjectType.FullName + "." + propertyInfoItem.Name;
                    PropertyInfoContainer container = new PropertyInfoContainer {
                        PropertyName = propertyInfoItem.Name, InstanceTypeHandle = serializeObjectType.TypeHandle, PropertyTypeHandle = propertyInfoItem.PropertyType.TypeHandle
                    };
                    object objectValue = propertyAccessorManager.GetPropertyAccessor(propertyKey, container).GetValue(serializeObject); //propertyInfoItem.GetValue(serializeObject);//
                    Serialize(objectValue, setting, true, valueSb, propertyAccessorManager, propertyInfoItem.Name);
                    propertyIndex++;
                    if (propertyIndex < propertyCount)
                    {
                        valueSb.Append(JsonSymbol.JsonSeparateSymbol);
                    }
                }
                valueSb.Append(JsonSymbol.JsonObjectSymbol_End);
            }
        }