首页 > 解决方案 > IEnumerable 不包含“长度”的定义

问题描述

 error CS1061: 'IEnumerable<Attribute>' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'IEnumerable<Attribute>' could be found (are you missing a using directive or an assembly reference?)

当我尝试使用 .Net 而不是 IL2CPP 进行编译时,使用 Unity 2018 时出现此错误 这是我收到错误的行:

 if (methods[i].GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Length != 0)
            {
                continue;
            }

并且,在另一种方法中:

  var ret = (Delegate.CreateDelegate(typeof(V), target, method) as V);

另外,在这种方法中,我使用了 methodinfo 而不是 Delegate。但随后询问没有使用 3 个值的方法。

还添加了“使用 system.Linq”

此致

标签: c#

解决方案


实际上,您想测试是否有任何项目在IEnumerable<T>

 if (methods[i].GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Any())
 {
     continue;
 }

如果您坚持Length != 0使用正确的语法是

 if (methods[i].GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Count() > 0)
 {
     continue;
 }

推荐阅读