Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper red underline on type inference in VS 2010

I'm getting a strange error in VS 2010. I have a project set to use .NET Framework 4. When I type 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 an enumerable of objects, converts them to strings using ToString(), and then auto-selects the string passed.

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

However, if I change the code to explicitly say the type:

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

VS 2010 no longer gives the error. My understand of generics is that these two fragments of code should be the same to the compiler, so why is it giving me an error underline?

Also:

This will also fix the problem:

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

ReSharper:

I've tried deleting my _ReSharper directory, but no luck. I still get the underline. However, if I suspend (i.e. turn off) ReSharper, the red underline goes away so it's definitely being caused by ReSharper.

Code:

As requested, here's the code.

Here's 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;
        }

        ...
    }
}
like image 997
cdmckay Avatar asked Jun 04 '11 23:06

cdmckay


3 Answers

I would guess that the return type is a List instead of an Ienumerable and that is what VS is crying about. I assume both code works, but it likes things to be explicit.

like image 187
hivie7510 Avatar answered Nov 20 '22 14:11

hivie7510


You have stated in the comments the project in question was previously targeting .NET3.5.

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

You could try

  1. Clear the Resharper cache (Resharper -> Options -> Environment/General -> Clear Caches)
  2. Recreate the project (csproj) file as a .NET4 project from the start
  3. Use Resharper 6.0 Beta (just out) as it doesnt appear to have this issue
like image 37
wal Avatar answered Nov 20 '22 15:11

wal


When you type in, VS does interpreter works. When it cannot determine the type of a variable , it gives you red underline error. This may occur as a bug of VS or its add-ins (such as Resharper, just my guess)

When you compile, C# compiler do all the hard works, it looks through all references and replace var with appropriate type, so red underline errors are gone.

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

like image 1
Quan Mai Avatar answered Nov 20 '22 14:11

Quan Mai