Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="level"></param>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        protected virtual ILogEntry CreateEntry(LogLevel level, object message, Exception exception)
        {
            if (message != null && message is Exception && exception == null)
            {
                return(CreateEntry(level, null, (Exception)message));
            }

            ILogEntry entry = null;

            if (message == null)
            {
                entry = this.CreateEntry();
            }
            else if (message is ILogEntry)
            {
                entry = (ILogEntry)message;
            }
            else if (message is IDictionary)
            {
                entry = this.CreateEntry();
                var items         = (IDictionary)message;
                var classAccessor = ClassAccessorRepository.GetClassAccessor(entry, AccessorType.Reflection);

                foreach (DictionaryEntry item in items)
                {
                    var key = item.Key.ToString();
                    if (classAccessor.PropertyAccessores.ContainsKey(key.ToLower()))
                    {
                        classAccessor.SetValue(entry, key, item.Value);
                    }
                    else
                    {
                        entry.ExtendInfo.Add(key, item.Value);
                    }
                }
            }
            else
            {
                entry         = this.CreateEntry();
                entry.Message = message.ToString();
            }

            entry.LogLevel = level;
            if (exception != null && entry.Exception == null)
            {
                entry.Exception = exception;
            }

            this.AppendExtendInfo(entry);

            return(entry);
        }
Пример #2
0
        private static object DeserializeObject(object target, IConfigNode configNode)
        {
            var targetType        = target.GetType();
            var propertyValues    = configNode.GetProperties();
            var accessor          = ClassAccessorRepository.GetClassAccessor(target, AccessorType.Reflection);
            var persistProperties = targetType.GetPersistProperties(AccessorType.Reflection);
            var targetProperties  = new Dictionary <string, Type>(persistProperties.Count);

            foreach (var item in persistProperties)
            {
                targetProperties.Add(item.Key.ToLower(), item.Value);
            }

            foreach (var pair in propertyValues)
            {
                var pName  = pair.Key;
                var pValue = pair.Value;

                var propertyName = string.Empty;
                var pType        = TryGetPropertyType(targetProperties, pName, out propertyName);
                if (pType == null)
                {
                    continue;
                }

                var targetValue = accessor.GetValue(target, propertyName);

                if (pType.IsListType())
                {
                    var targetList = targetValue as IList;
                    if (targetList == null)
                    {
                        targetList = (IList)pType.BuildObject();
                        accessor.SetValue(target, propertyName, targetList);
                    }

                    IList valList = null;
                    if (pValue is IList)
                    {
                        valList = (IList)pValue;
                    }
                    else
                    {
                        valList = new List <object>(1)
                        {
                            pValue
                        };
                    }
                    DeserializeList(pType, targetList, valList);
                }
                else if (pType.IsDictionaryType())
                {
                    var targetMap = targetValue as IDictionary;
                    if (targetMap == null)
                    {
                        targetMap = (IDictionary)pType.BuildObject();
                        accessor.SetValue(target, propertyName, targetMap);
                    }
                    var valMap = configNode.GetNodeMap(pName);
                    DeserializeDictionary(pType, targetMap, valMap);
                }
                else
                {
                    if (pValue is IConfigNode)
                    {
                        var val = BuildObject(pType, (IConfigNode)pValue);
                        accessor.SetValue(target, propertyName, val);
                    }
                    else
                    {
                        var val = DeserializePrimitiveValue(pValue, pType);
                        accessor.SetValue(target, propertyName, val);
                    }
                }
            }

            return(target);
        }