开发者

ReSharper red underline on type inference in VS 2010

开发者 https://www.devze.com 2023-03-10 16:52 出处:网络
I\'m getting a strange error in VS 2010.I have a project set to use .NET Framework 4.When I type the code:

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 S开发者_如何学编程taffRepository().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;
        }

        ...
    }
}


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.


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


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


The problem is that CreateSelectList takes IEnumerable<Object>, not IEnumerable<Staff>. Even though Staff inherits from Object, the IEnumerable<Staff> does not inherit from IEnumerable<Object>. This is a common mistake that people make with generics.

I'd suggest changing the signature of CreateSelectList to:

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


unloading and reloading project helped me to clear out this issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消