首页 > 解决方案 > 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>>>

所以当类型是列表或字典时我遇到的问题是一个可枚举的。

我希望他很清楚

谢谢你的帮助

皮尔卡洛

标签: c#reflection

解决方案


推荐阅读