c# - type.GenericTypeArguments 的意外结果
问题描述
通过反思,我成功地获得了控制器方法的列表。
要获取我以这种方式使用 *.GenericTypeArguments 的方法的返回类型的参数:
Some MethodInfo x.GenericTypeArguments.Select(a => a.Name);
当方法是这样的:
public async Task<ApiResult> SomeMethod();
我得到了很好的结果:“ApiResult”
但是当返回类型有一个类似的列表时:
public async Task<List<string>> SomeMethod();
我得到 -> Task,List,System.Linq.Enumerable+SelectArrayIterator`2[System.Type,System.String] 而不是 Task,List,String
同样的
public async Task<Dictionary<string,string>> SomeMethod();
预期任务,字典,字符串,字符串
检索到 Task,Dictionary,System.Linq.Enumerable+SelectArrayIterator`2[System.Type,System.String]
here the exact code I use:
Assembly asm = Assembly.GetExecutingAssembly();
var controllerMethodlist = asm.GetTypes()
.Where(type => typeof(ControllerBase).IsAssignableFrom(type))
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
.Where(m => !m.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Any())
.Select(x => new
{
Controller = x.DeclaringType.Name,
Action = x.Name + "(" + String.Join(",", x.GetParameters().Select(p => p.Name)) + ")",
ReturnType = x.ReturnType.Name.LastIndexOf('`') == -1 ? x.ReturnType.Name : (x.ReturnType.Name.Substring(0, x.ReturnType.Name.LastIndexOf('`')) + "<" +
String.Join(",", x.ReturnType.GenericTypeArguments.Select(a => a.Name.LastIndexOf('`') == -1 ? a.Name : a.Name.Substring(0, a.Name.LastIndexOf('`')) +
(a.GenericTypeArguments.Count() > 0 ? "<" + String.Join(",", a.GenericTypeArguments.Select(b => b.Name.LastIndexOf('`') == -1 ? b.Name : b.Name.Substring(0, b.Name.LastIndexOf('`')) +
(b.GenericTypeArguments.Count() > 0 ? "<" + String.Join(",", b.GenericTypeArguments.Select(c => c.Name.LastIndexOf('`') == -1 ? c.Name : c.Name.Substring(0, c.Name.LastIndexOf('`'))) + ">") : ""))) + ">" : ""))) + ">")
,
Attributes = String.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute", "") + "|" +
(
a.GetType() == typeof(HttpGetAttribute) ? ((HttpGetAttribute)a).Template :
a.GetType() == typeof(HttpPostAttribute) ? ((HttpPostAttribute)a).Template :
a.GetType() == typeof(HttpDeleteAttribute) ? ((HttpDeleteAttribute)a).Template :
a.GetType() == typeof(HttpPutAttribute) ? ((HttpPutAttribute)a).Template :
a.GetType() == typeof(CrudControllerAuthAttribute) ? ((CrudControllerAuthAttribute)a).CrudAuth.ToString() :
"*"
)))
})
.OrderBy(x => x.Controller).ThenBy(x => x.Action).ToList();
问题出在“ReturnType”属性中:
我有以下方法:
public async Task<IActionResult> ConfirmEmail(parameters)
作为 ResultType 我得到了正确的值
Task<IActionResult>,
但使用以下方法:
public async Task<ApiResultT<Dictionary<string,string>>> GetCsvDictionaryForTemplate(CsvBase64)
作为 ResultType 我得到
`Task<ApiResultT<Dictionary<System.Linq.Enumerable+SelectArrayIterator`2[System.Type,System.String]>>>`
代替
Task<ApiResultT<Dictionary<string,string>>>
或使用以下方法:
public async Task<ApiResultT<List<CsvClassesDto>>> GetCsvClasses()
我越来越:
Task<ApiResultT<List<System.Linq.Enumerable+SelectArrayIterator`2[System.Type,System.String]>>>
代替:
Task<ApiResultT<List<CsvClassesDto>>>
所以当类型是列表或字典时我遇到的问题是一个可枚举的。
我希望他很清楚
谢谢你的帮助
皮尔卡洛
解决方案
推荐阅读
- python - 如何创建一个从元组列表中获取随机元组并在 python 中生成基本图的程序?
- linux - 在新版本中使用 docker rootless
- php - Codeigniter - 如何获取为其提供重复值的列名
- angular - 错误的重定向到本地主机
- haskell - Haskell 从 bmp Image 获取矩阵
- api - Netscalar 重定向请求到 OpenShift 路由
- laravel - 如何从控制器显示数据到刀片视图以及如何从远程 api 进行路由?拉拉维尔
- php - 如何在代码点火器中输出外键数据
- python - 如何使用烧瓶在应用程序中返回 API 对象
- c# - 如何在 C# 中使用命令提示符运行 deepfreeze 命令行