public TypeDefinition GetOrCreate(Type type) { TypeDefinition def; if (_cache == null) { _generator = new JsonSchemaGenerator(); if (_supportRecursive) _generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseAssemblyQualifiedName; _cache = new Dictionary<Type, TypeDefinition>(); } if (!_cache.TryGetValue(type, out def)) { def = new TypeDefinition(type, _generator, this); _cache.Add(type, def); } return def; }
public void ScanTypeDefinition(HashSet<Type> parentCustomTypes) { _def = Container.GetOrCreate(_type); if (_def.IsObject) { var properties = _type.GetProperties(); _properties = new List<PropertyDescriptor>(properties.Length); _propertiesDic = new Dictionary<string, PropertyDescriptor>(properties.Length, StringComparer.CurrentCultureIgnoreCase); if (_def.IsCustomObject) { if (parentCustomTypes.Contains(_type)) return; parentCustomTypes.Add(_type); } if (!TypeDefinition.IsDictionaryType(_type)) { foreach (var property in properties) { if (property.GetCustomAttributes(typeof(JsonIgnoreAttribute), false).Length > 0) continue; var item = new PropertyDescriptor(property, _depth + 1); item.Container = Container; _properties.Add(item); _propertiesDic.Add(property.Name, item); item.ScanTypeDefinition(new HashSet<Type>(parentCustomTypes)); } } } else if (_def.IsArray && _def.SubType != null) { var subDef = Container.GetOrCreate(_def.SubType); _subType = new TypeDescriptor(_def.SubType.Name, _def.SubType, _depth); _subType.Container = Container; _subType.ScanTypeDefinition(parentCustomTypes); } }