1、获取枚举集合(根据枚举类名通过反射实现)
/// <summary>
/// 枚举下拉列表接口
/// </summary>
/// <returns></returns>
[HttpGet("enumerations/{enumCode}")]
[AllowAnonymous]
public IEnumerable<SelectListItemDto> GetProjectTypes(string enumCode)
{
var generalInfoAppService = LazyServiceProvider.LazyGetRequiredService<IGeneralInfoAppService>();
return generalInfoAppService.GetSelectListForEnumeration(enumCode, typeof(SupplyStatus).Assembly.FullName);
}
2、接口定义
using System.Reflection;
using Tzkj.Application.Contracts.Dtos;
using Tzkj.Core.DomainObjects;
using Volo.Abp.Application.Services;namespace Tzkj.Application.Contracts;public interface IGeneralInfoAppService : IApplicationService
{IEnumerable<SelectListItemDto> GetSelectListForEnumeration(string enumCode, string domainSharedAssemblyFullName);string GetStorageFullUrl(string storageName);
}
3、接口实现方法
using Microsoft.Extensions.Options;
using System;
using System.Reflection;
using System.Security.AccessControl;
using System.Text.RegularExpressions;
using Tzkj.Application.Contracts;
using Tzkj.Application.Contracts.Dtos;
using Tzkj.BlobStorings.Abstractions;
using Tzkj.Core.DomainObjects;
using Tzkj.Core.Enums;
using Tzkj.Core.Enums.Approvals;
using Volo.Abp;namespace Tzkj.Application;public class GeneralInfoAppService : AppServiceBase, IGeneralInfoAppService
{private readonly MinioOptions _minioOptions;public GeneralInfoAppService(IOptions<MinioOptions> options){_minioOptions=options.Value;}public IEnumerable<SelectListItemDto> GetSelectListForEnumeration(string enumCode, string domainSharedAssemblyFullName){var coreAssembly = typeof(ApproveTrigger).Assembly;var domainSharedAssembly = Assembly.Load(domainSharedAssemblyFullName);var allAssemblies = new List<Assembly> { domainSharedAssembly, coreAssembly };var baseType = typeof(Enumeration<>);var derivedTypes = allAssemblies.SelectMany(a => a.GetTypes()).Where(t => t.BaseType != null&& t.BaseType.Name == typeof(Enumeration<>).Name);var curEnumType = derivedTypes.FirstOrDefault(derivedType => enumCode == derivedType.Name);if (curEnumType is null)throw new UserFriendlyException($"参数{nameof(enumCode)}值{enumCode}不是有效的枚举名称");var method = curEnumType!.BaseType!.BaseType!.GetMethod("GetSelectItems", BindingFlags.Public | BindingFlags.Static);var retObject = method!.Invoke(null, null);return ((retObject as List<SelectItemDo>)!).Select(i=>new SelectListItemDto(i.Value,i.Text));}public string GetStorageFullUrl(string storageName){if (storageName.IsNullOrWhiteSpace())return storageName;var endpoint = _minioOptions.OutSideEndpoint.Split(':');var host = endpoint[0];var port = (endpoint.Length > 1 ? endpoint[1] : "80").To<int>();if (storageName.StartsWith("http://") || storageName.StartsWith("https://")){var uriBuilder = new UriBuilder(storageName);var pattern = @"\b(?:\d{1,3}\.){3}\d{1,3}(?::\d{1,5})?\b";if (!Regex.IsMatch(uriBuilder.Host, pattern))return storageName;if (uriBuilder.Host == host && uriBuilder.Port == port) return storageName;uriBuilder.Host=host;uriBuilder.Port = port;return uriBuilder.ToString();}storageName = storageName.TrimStart('/');var builder = new UriBuilder("http", host, port);var paths = new List<string> { _minioOptions.BucketName, "host", storageName };builder.Path = Path.Combine(paths.ToArray());return builder.ToString();}
}
public IEnumerable<SelectListItemDto> GetSelectListForEnumeration(string enumCode, string domainSharedAssemblyFullName){var coreAssembly = typeof(ApproveTrigger).Assembly;var domainSharedAssembly = Assembly.Load(domainSharedAssemblyFullName);var allAssemblies = new List<Assembly> { domainSharedAssembly, coreAssembly };var baseType = typeof(Enumeration<>);var derivedTypes = allAssemblies.SelectMany(a => a.GetTypes()).Where(t => t.BaseType != null&& t.BaseType.Name == typeof(Enumeration<>).Name);var curEnumType = derivedTypes.FirstOrDefault(derivedType => enumCode == derivedType.Name);if (curEnumType is null)throw new UserFriendlyException($"参数{nameof(enumCode)}值{enumCode}不是有效的枚举名称");var method = curEnumType!.BaseType!.BaseType!.GetMethod("GetSelectItems", BindingFlags.Public | BindingFlags.Static);var retObject = method!.Invoke(null, null);return ((retObject as List<SelectItemDo>)!).Select(i=>new SelectListItemDto(i.Value,i.Text));}
注意:
以上根据枚举类名通过反射实现,且使用懒加载,注入的IGeneralInfoAppService服务,且传递的枚举类必须在项目下,在该解决方案不同的项目下是查询不出来的