1
 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?)

I have this error using Unity 2018 when i try to compile with .Net instead of IL2CPP This is the line where i get the error:

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

And, in this other method:

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

also, in this method i used methodinfo instead of Delegate. but then ask about no method using 3 values.

Also added "using system.Linq"

Best regards

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
pacogp
  • 11
  • 3
  • 1
    What is your question? – O. R. Mapper Nov 06 '18 at 11:15
  • 2
    `.Count()` or `.Any()` is likely what you want. – mjwills Nov 06 '18 at 11:17
  • `.Count()` instead of `Length`; or (better) `.Any()` - if any items in the `IEnumerable` – Dmitry Bychenko Nov 06 '18 at 11:17
  • This is because ```IEnumerable``` isn't a length of anything. It's just an interface with one method ```GetEnumerator()```. It's commonly confused that the ```IEnumerable``` is an array of some type with a fixed length but this does not have to be and often isn't the case. ```Array``` and ```List``` do use ```IEnumerable``` interfaces but the interface itself does not imply any items exist. – Michael Puckett II Nov 06 '18 at 11:21

2 Answers2

4

Actually you want to test if any items are in the IEnumerable<T>:

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

If you insist on Length != 0 approach the right syntax is

 if (methods[i].GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Count() > 0)
 {
     continue;
 }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • @Rahul: Thank you! Technically, `!= 0` and `> 0` are equal in the context; but `> 0` is more readable. – Dmitry Bychenko Nov 06 '18 at 11:21
  • 3
    The first one, ```.Any()``` is recommended as it returns ```true``` as soon as the result is ```true```; which in this case would be any first iteration. ```Count()``` will enumerate the entirety and return a value to compare against which is much slower. – Michael Puckett II Nov 06 '18 at 11:24
  • 1
    It's worth noting that some enumerations don't even contain objects but create them on enumeration which would make ```Count``` and ```Any``` both slow but ```Count``` much slower. – Michael Puckett II Nov 06 '18 at 11:26
-1

Ok, right now this works fine:

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

Right now, the other error in:

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

I try to use MethodInfo, but isn´t working. 'Delegate' does not contain a definition for 'CreateDelegate'

pacogp
  • 11
  • 3
  • 1
    That is a sufficiently different question @pacogp - please write a new question rather than discuss it here. – mjwills Nov 06 '18 at 11:59