Exemplo n.º 1
0
 /// <summary>
 /// 添加属性容器
 /// </summary>
 /// <param name="propertyKey"></param>
 /// <param name="container"></param>
 public void AddPropertyInfoContainer(string propertyKey, PropertyInfoContainer container)
 {
     if (!_DicContainer.ContainsKey(propertyKey))
     {
         _DicContainer.TryAdd(propertyKey, container);
     }
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 根据属性Key获取属性访问器
        /// </summary>
        /// <param name="propertyKey"></param>
        /// <param name="container"></param>
        /// <returns></returns>
        public IPropertyAccessor GetPropertyAccessor(string propertyKey, PropertyInfoContainer container)
        {
            IPropertyAccessor accessor = null;

            _DicPropertyAccessor.TryGetValue(propertyKey, out accessor);
            if (accessor == null)
            {
                accessor = Activator.CreateInstance(typeof(PropertyAccessor <,>).MakeGenericType(container.InstanceType, container.PropertyType), container.PropertyInfoContainerToPropertyInfo()) as IPropertyAccessor;
                _DicPropertyAccessor.TryAdd(propertyKey, accessor);
            }
            return(accessor);
        }
 public static PropertyInfo PropertyInfoContainerToPropertyInfo(this PropertyInfoContainer container)
 {
     return(container.InstanceType.GetProperty(container.PropertyName));
 }
Exemplo n.º 5
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);
            }
        }