using System; using System.Collections.Generic; using System.Reflection; using System.Text; namespace Apewer.Web { /// API 行为的参数。 public sealed class ApiParameter : IToJson { ParameterInfo _parameter = null; bool _body = false; bool _query = false; /// public ParameterInfo ParameterInfo { get => _parameter; } /// public string Name { get => _parameter.Name; } /// public Type Type { get => _parameter.ParameterType; } /// public bool FromBody { get => _body; } /// public bool FromQuery { get => _query; } /// public override string ToString() => _parameter.Name; /// public Json ToJson() { var format = ApiActionJsonFormat.Default ?? new ApiActionJsonFormat(); var withReflection = format.WithReflection; return ToJson(withReflection); } /// public Json ToJson(bool withReflection) { var json = Json.NewObject(); json.SetProperty("name", _parameter.Name); if (withReflection) { json.SetProperty("type", _parameter.ParameterType.Name); } if (_body) json.SetProperty("fromBody", _body); if (_query) json.SetProperty("fromQuery", _query); return json; } /// /// ApiParameter(ParameterInfo parameterInfo) { _parameter = parameterInfo ?? throw new ArgumentNullException(nameof(parameterInfo)); _query = RuntimeUtility.Contains(parameterInfo); _body = RuntimeUtility.Contains(parameterInfo); } /// public static ApiParameter Parse(ParameterInfo parameter) { if (parameter == null) return null; if (parameter.IsOut) return null; if (parameter.IsRetval) return null; return new ApiParameter(parameter); } } }