public EFJavaScriptConverter(int maxDepth = 1, EFJavaScriptConverter parent = null)
 {
     _maxDepth = maxDepth;
     if (parent != null)
     {
         _currentDepth += parent._currentDepth;
     }
 }
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            _processedObjects.Add(obj.GetHashCode());
            var type = obj.GetType();

            var properties = from p in type.GetProperties()
                             where p.CanRead && p.GetIndexParameters().Count() == 0 &&
                                   _builtInTypes.Contains(p.PropertyType)
                             select p;

            var result = properties.ToDictionary(
                          p => p.Name,
                          p => (Object)TryGetStringValue(p, obj));

            if (_maxDepth >= _currentDepth)
            {
                var complexProperties = from p in type.GetProperties()
                                        where p.CanRead &&
                                              p.GetIndexParameters().Count() == 0 &&
                                              !_builtInTypes.Contains(p.PropertyType) &&
                                              p.Name != "RelationshipManager" &&
                                              !AllreadyAdded(p, obj)
                                        select p;

                foreach (var property in complexProperties)
                {
                    var complexValue = TryGetValue(property, obj);

                    if (complexValue != null)
                    {
                        var js = new EFJavaScriptConverter(_maxDepth - _currentDepth, this);

                        result.Add(property.Name, js.Serialize(complexValue, new EFJavaScriptSerializer()));
                    }
                }
            }

            return result;
        }