ReSharper red underline by type of output in VS 2010

I get a strange error in VS 2010. I have a project using the .NET Framework 4. When I type in the code:

var record = ...; // returns IEnumerable<Staff> var staff = new StaffRepository().GetAll(); // The method has two signatures: // CreateSelectList(IEnumerable<object> enumerable, string value) // CreateSelectList(IDictionary<object, object> enumerable, string value) StaffList = SelectListHelper.CreateSelectList(staff, record.Staff); 

CreateSelectList basically takes enumerated objects, converts them to strings using ToString() , and then automatically selects the passed string.

The problem is that this code gets a red underline in VS 2010 with an error saying that it cannot solve this method.

However, if I changed the code to explicitly specify the type:

 IEnumerable<Staff> staff = new StaffRepository().GetAll(); StaffList = SelectListHelper.CreateSelectList(staff, record.Staff); 

VS 2010 no longer gives an error. My understanding of generics is that these two code snippets should be the same with the compiler, so why does this give me an error?

AND

This will also fix the problem:

 var staff = new StaffRepository().GetAll(); StaffList = SelectListHelper.CreateSelectList(from s in staff select s, record.Staff); 

ReSharper:

I tried to delete my _ReSharper directory, but no luck. I am still getting underscore. However, if I paused (i.e., turned off) ReSharper, the red highlight will disappear, so it is definitely called ReSharper.

The code:

As requested, here is the code.

Here's the StaffRepository:

 namespace Foo.Client.Business.Repositories { public class StaffRepository : StaffRepositoryBase<Staff, StaffCriteria, Discriminator> { public StaffRepository(Discriminator discriminator) : base(discriminator) { } protected override Staff CreateStaff(MembershipUser user) { return new Staff(user); } } // end class } 

Here's StaffRepositoryBase (where GetAll is defined):

 namespace Foo.Common.Business.Repositories { public abstract class StaffRepositoryBase<TStaff, TStaffCriteria, TDiscriminator> : IStaffRepository<TStaff, TStaffCriteria, TDiscriminator> where TStaff : class, IStaff<TDiscriminator> where TStaffCriteria : class, IStaffCriteria { ... protected abstract TStaff CreateStaff(MembershipUser user); public virtual IEnumerable<TStaff> GetAll() { return from u in Membership.GetAllUsers().Cast<MembershipUser>() let s = CreateStaff(u) where s.Discriminator.Equals(Discriminator) select s; } public virtual IEnumerable<TStaff> GetAll(LimitCriteria criteria) { var staffs = GetAll() .Skip(criteria.Skip.GetValueOrDefault()) .Take(criteria.Take.GetValueOrDefault()); return staffs; } public virtual IEnumerable<TStaff> GetAll() { return from u in Membership.GetAllUsers().Cast<MembershipUser>() let s = CreateStaff(u) where s.Discriminator.Equals(Discriminator) select s; } ... } } 
+6
source share
5 answers

You pointed out in the comments that this project was previously targeted at .NET3.5.

For a project targeting .NET 3.5, Resharper will be underlined in the order you describe. Given that you are targeting .NET4 right now, Resharper still thinks you're in .NET3.5 for some reason.

You may try

  • Clear Resharper Cache ( Resharper -> Options -> Environment/General -> Clear Caches )
  • Create the project file (csproj) again as a .NET4 project
  • Use Resharper 6.0 Beta (just out), as it doesn't seem to have this problem.
+2
source

I would suggest that the return type is List instead of Ienumerable, and this is what VS is talking about. I assume both codes work, but he likes things to be explicit.

+3
source

On entering, VS executes the interpreter. When it cannot determine the type of variable, it gives a red underlined error. This can happen as a bug of VS or its add-ons (such as Resharper, just my hunch)

When you compile, the C # compiler does all the hard work, it scans all the links and replaces var with the appropriate type, so the red flaws are underlined.

You can try to clear your solution, close VS and restart it, it can help or not, but it's worth a try

+1
source

The problem is that CreateSelectList accepts an IEnumerable<Object> , not an IEnumerable<Staff> . Although Staff inherits from Object, IEnumerable<Staff> does not inherit from IEnumerable<Object> . This is a common mistake that people make with generics.

I would suggest changing the signature of CreateSelectList to:

 CreateSelectList<T>(IEnumerable<T> enumerable, string value) 
0
source

The unload and reload project helped me fix this problem.

0
source

Source: https://habr.com/ru/post/889794/


All Articles