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;
}
...
}
}
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
Resharper -> Options -> Environment/General -> Clear Caches
) 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With