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