private static void AddCustomTypesToModel(ApplicationApiDescriptionModel applicationModel, [CanBeNull] Type type) { if (type == null) { return; } type = AsyncHelper.UnwrapTask(type); if (type == typeof(object) || type == typeof(void) || type == typeof(Enum) || type == typeof(ValueType) || TypeHelper.IsPrimitiveExtended(type)) { return; } if (TypeHelper.IsEnumerable(type, out var itemType)) { AddCustomTypesToModel(applicationModel, itemType); return; } if (TypeHelper.IsDictionary(type, out var keyType, out var valueType)) { AddCustomTypesToModel(applicationModel, keyType); AddCustomTypesToModel(applicationModel, valueType); return; } /* TODO: Add interfaces */ var typeName = TypeHelper.GetFullNameHandlingNullableAndGenerics(type); if (applicationModel.Types.ContainsKey(typeName)) { return; } var typeModel = TypeApiDescriptionModel.Create(type); applicationModel.Types[typeName] = typeModel; AddCustomTypesToModel(applicationModel, type.BaseType); foreach (var propertyInfo in type.GetProperties()) { AddCustomTypesToModel(applicationModel, propertyInfo.PropertyType); } }
public static TypeApiDescriptionModel Create(Type type) { var baseType = type.BaseType; if (baseType == typeof(object)) { baseType = null; } var typeModel = new TypeApiDescriptionModel { IsEnum = type.IsEnum, BaseType = baseType != null?TypeHelper.GetFullNameHandlingNullableAndGenerics(baseType) : null }; if (typeModel.IsEnum) { typeModel.EnumNames = type.GetEnumNames(); typeModel.EnumValues = type.GetEnumValues().Cast <object>().ToArray(); } else { typeModel.Properties = type .GetProperties() .Where(p => p.DeclaringType == type) .Select(PropertyApiDescriptionModel.Create) .ToArray(); if (type.IsGenericTypeDefinition) { typeModel.GenericArguments = type.GetGenericArguments().Select(a => a.Name).ToArray(); } } return(typeModel); }
private static void AddCustomTypesToModel(ApplicationApiDescriptionModel applicationModel, [CanBeNull] Type type) { if (type == null) { return; } if (type.IsGenericParameter) { return; } type = AsyncHelper.UnwrapTask(type); if (type == typeof(object) || type == typeof(void) || type == typeof(Enum) || type == typeof(ValueType) || TypeHelper.IsPrimitiveExtended(type)) { return; } if (TypeHelper.IsDictionary(type, out var keyType, out var valueType)) { AddCustomTypesToModel(applicationModel, keyType); AddCustomTypesToModel(applicationModel, valueType); return; } if (TypeHelper.IsEnumerable(type, out var itemType)) { AddCustomTypesToModel(applicationModel, itemType); return; } if (type.IsGenericType && !type.IsGenericTypeDefinition) { var genericTypeDefinition = type.GetGenericTypeDefinition(); AddCustomTypesToModel(applicationModel, genericTypeDefinition); foreach (var genericArgument in type.GetGenericArguments()) { AddCustomTypesToModel(applicationModel, genericArgument); } return; } var typeName = CalculateTypeName(type); if (applicationModel.Types.ContainsKey(typeName)) { return; } applicationModel.Types[typeName] = TypeApiDescriptionModel.Create(type); AddCustomTypesToModel(applicationModel, type.BaseType); foreach (var propertyInfo in type.GetProperties().Where(p => p.DeclaringType == type)) { AddCustomTypesToModel(applicationModel, propertyInfo.PropertyType); } }