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; } ... } }