为了账号安全,请及时绑定邮箱和手机立即绑定

此代码中反射的输出类型是什么

此代码中反射的输出类型是什么

C#
翻翻过去那场雪 2022-12-24 12:46:03
我有以下方法:public static class ReflectionHelper{    public static List<?> FindType<T>()    {        var A =            from Assemblies in AppDomain.CurrentDomain.GetAssemblies().AsParallel()            from Types in Assemblies.GetTypes()            let Attributes = Types.GetCustomAttributes(typeof(T), true)            where Attributes?.Length > 0            select new { Type = Types };        var L = A.ToList();        return L;    }}列表的类型是什么?如果我做:foreach (var l in L) { ... }它可以找到并且我可以检查类型,但是我正在使用的开发环境 (Rider) 不会提供类型
查看完整描述

1 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

它是一个具有单一属性的匿名对象


IEnumerable<Type> Types;

所以,使用A.ToList()给你一个匿名对象的列表,你不能返回。


我认为select new { Type = Types };你想使用而不是使用select Types;


所以:


public static List<Type> FindType<T>()

{

    var types =

        from ssembly in AppDomain.CurrentDomain.GetAssemblies().AsParallel()

        from type in ssembly.GetTypes()

        let attributes = type.GetCustomAttributes(typeof(T), true)

        where attributes?.Length > 0

        select type;


    return types.ToList();

}


查看完整回答
反对 回复 2022-12-24
  • 1 回答
  • 0 关注
  • 54 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信