Пример #1
0
        public ActionResult <GenericResponse <TypeResponse> > TypeGet(string type)
        {
            if (string.IsNullOrEmpty(type))
            {
                ASF.ArchiLogger.LogNullError(nameof(type));
                return(BadRequest(new GenericResponse <TypeResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(type)))));
            }

            Type targetType = WebUtilities.ParseType(type);

            if (targetType == null)
            {
                return(BadRequest(new GenericResponse <object>(false, string.Format(Strings.ErrorIsInvalid, type))));
            }

            string           baseType         = targetType.BaseType?.GetUnifiedName();
            HashSet <string> customAttributes = targetType.CustomAttributes.Select(attribute => attribute.AttributeType.GetUnifiedName()).ToHashSet();
            string           underlyingType   = null;

            Dictionary <string, string> body = new Dictionary <string, string>();

            if (targetType.IsClass)
            {
                foreach (FieldInfo field in targetType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(field => !field.IsPrivate))
                {
                    JsonPropertyAttribute jsonProperty = field.GetCustomAttribute <JsonPropertyAttribute>();

                    if (jsonProperty != null)
                    {
                        body[jsonProperty.PropertyName ?? field.Name] = field.FieldType.GetUnifiedName();
                    }
                }

                foreach (PropertyInfo property in targetType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(property => property.CanRead && !property.GetMethod.IsPrivate))
                {
                    JsonPropertyAttribute jsonProperty = property.GetCustomAttribute <JsonPropertyAttribute>();

                    if (jsonProperty != null)
                    {
                        body[jsonProperty.PropertyName ?? property.Name] = property.PropertyType.GetUnifiedName();
                    }
                }
            }
            else if (targetType.IsEnum)
            {
                Type enumType = Enum.GetUnderlyingType(targetType);
                underlyingType = enumType.GetUnifiedName();

                foreach (object value in Enum.GetValues(targetType))
                {
                    body[value.ToString()] = Convert.ChangeType(value, enumType).ToString();
                }
            }

            TypeResponse.TypeProperties properties = new TypeResponse.TypeProperties(baseType, customAttributes.Count > 0 ? customAttributes : null, underlyingType);

            TypeResponse response = new TypeResponse(body, properties);

            return(Ok(new GenericResponse <TypeResponse>(response)));
        }
Пример #2
0
        private static async Task <bool> HandleApiTypeGet(HttpListenerRequest request, HttpListenerResponse response, string[] arguments, byte argumentsIndex)
        {
            if ((request == null) || (response == null) || (arguments == null) || (argumentsIndex == 0))
            {
                ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(arguments) + " || " + nameof(argumentsIndex));
                return(false);
            }

            if (arguments.Length <= argumentsIndex)
            {
                return(false);
            }

            string argument   = WebUtility.UrlDecode(arguments[argumentsIndex]);
            Type   targetType = Type.GetType(argument);

            if (targetType == null)
            {
                await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorIsInvalid, nameof(argument))), HttpStatusCode.BadRequest).ConfigureAwait(false);

                return(true);
            }

            string           baseType         = targetType.BaseType?.GetUnifiedName();
            HashSet <string> customAttributes = new HashSet <string>(targetType.CustomAttributes.Select(attribute => attribute.AttributeType.GetUnifiedName()));
            string           underlyingType   = null;

            Dictionary <string, string> body = new Dictionary <string, string>();

            if (targetType.IsClass)
            {
                foreach (FieldInfo field in targetType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(field => !field.IsPrivate))
                {
                    body[field.Name] = field.FieldType.GetUnifiedName();
                }

                foreach (PropertyInfo property in targetType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(property => property.CanRead && !property.GetMethod.IsPrivate))
                {
                    body[property.Name] = property.PropertyType.GetUnifiedName();
                }
            }
            else if (targetType.IsEnum)
            {
                Type enumType = Enum.GetUnderlyingType(targetType);
                underlyingType = enumType.GetUnifiedName();

                foreach (object value in Enum.GetValues(targetType))
                {
                    body[value.ToString()] = Convert.ChangeType(value, enumType).ToString();
                }
            }

            TypeResponse.TypeProperties properties = new TypeResponse.TypeProperties(baseType, customAttributes.Count > 0 ? customAttributes : null, underlyingType);

            await ResponseJsonObject(request, response, new GenericResponse(true, "OK", new TypeResponse(body, properties))).ConfigureAwait(false);

            return(true);
        }