6

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

        ...
    }
}
cdmckay
  • 31,832
  • 25
  • 83
  • 114
  • What's the return type of `GetAll` method? – Dyppl Jun 04 '11 at 23:26
  • Are you sure you're fixing it by typing `IEnumerable staff`, not `IEnumerable staff`? See http://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-4-0 – GSerg Jun 04 '11 at 23:47
  • Yup, it's definitely IEnumerable. I'm also using ReSharper if that has anything to do with it. – cdmckay Jun 04 '11 at 23:58
  • Is the return type declared as IEnumerable or some class implementing that interface? – Rune FS Jun 05 '11 at 00:12
  • Just to clarify, the code doesn't actually compile? Or is it just Intellisense. Im trying on actually reproducing your problem, but no matter what combination of explicitly implemented interfaces I use, VS won't complain. – J. Tihon Jun 05 '11 at 00:23
  • @J. Tihon: The code works fine, it's just the red underline that is bothering me. – cdmckay Jun 05 '11 at 00:25
  • @Rune FS: `StaffRepository` inherits from an abstract class `RepositoryBase` where `T` is set to `Staff`. The `GetAll` method is defined in the base class as `IEnumerable GetAll()`. I'm beginning to think this is the source of VS 2010's problem. – cdmckay Jun 05 '11 at 00:27
  • Have you "cleaned" your solution and restart Visual Studio? Or is this code used in a ASPX or Razor View? There might also be something else in your code which is throwing Intellisense off; maybe even a Resharper issue. Did you recently made any changes to those methods, which Intellisense hasn't picked up yet? – J. Tihon Jun 05 '11 at 00:33
  • Just did a Clean Solution and restart with no luck. Still the underline. Code is in a regular .cs file. – cdmckay Jun 05 '11 at 05:40
  • @cdmckay: are you by any chance using a Resharper? Sometimes it will act like that. If you do, try turning it off and see if the read underline is still there. IF it's gone, then clear R# cache files – Dyppl Jun 05 '11 at 07:14
  • And please, just give us the code of `GetAll` method – Dyppl Jun 05 '11 at 07:15
  • @Dyppl: I've updated the post with the code. – cdmckay Jun 05 '11 at 21:23
  • Ok, I tried disabling ReSharper and the problem goes away, so it's definitely ReSharper. – cdmckay Jun 14 '11 at 01:59
  • @cdmckay: what version of R# please? EAP build? – wal Jun 14 '11 at 13:54
  • @wal: JetBrains ReSharper 5.1 C# Edition Build 5.1.3000.12 on 2011-01-28T05:05:56 – cdmckay Jun 14 '11 at 19:09
  • @cdmckay: At any stage was the project targeting .NET3.5? If the project is not too large you could *try* recreating the project (csproj) file as a .NET4 from the start. – wal Jun 15 '11 at 01:24
  • @wal: Yeah the project was upgraded from .NET 3.5. Would that affect ReSharper? – cdmckay Jun 15 '11 at 03:40
  • @cdmckay Absolutely it does affect ReSharper.(it is aware of language differences between 3.5 and 4 when showing hints/red underlines) Somewhere its still reading .NET3.5 is my guess. – wal Jun 15 '11 at 06:13
  • @cdmckay I created a project with similar code as above targeting 3.5 and r# underlined the issue I then upgraded to 4 and the project compiled and r# stopped underlining - ie everything worked properly. Thats not to say everything should work your end, just letting you know. I still reckon recreating the csproj would fix it. You could also clear r# and VS temp files and/or edit the csproj file itself and look for any stray 3.5 ref's. – wal Jun 15 '11 at 12:02

5 Answers5

3

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.

hivie7510
  • 1,246
  • 10
  • 23
2

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
wal
  • 17,409
  • 8
  • 74
  • 109
1

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

Delta76
  • 13,931
  • 30
  • 95
  • 128
0

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

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
0

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)
Chris Shain
  • 50,833
  • 6
  • 93
  • 125