59

I have a base class that has a bool property which looks like this:

public abstract class MyBaseClass
{
     public bool InProgress { get; protected set; }
}

I am inheriting it another class from it and trying to add InProgress as a delegate to the dictionary. But it throws me an error. This is how my Derived class looks like:

public abstract class MyClass
{
     Dictionary<string, object> dict = new Dictionary<string, object>();
     dict.Add("InProgress", InProgress => base.InProgress = InProgress);

}

This is the error I am getting:

Cannot convert lambda expression to type 'object' because it is not a delegate type

What am I doing wrong here?

wonea
  • 4,783
  • 17
  • 86
  • 139
Asdfg
  • 11,362
  • 24
  • 98
  • 175
  • 9
    Exactly as the error message says - you've got a lambda expression and you're trying to convert it to `object`. Which delegate type did you *want* to convert it to, and how did you expect the compiler to know that? – Jon Skeet Apr 23 '14 at 14:19
  • 1
    Why are you trying to add a lambda to a dictionary? – D Stanley Apr 23 '14 at 14:19
  • 6
    @DStanley It's not that unusual, is it? – Rawling Apr 23 '14 at 14:58
  • @Rawling I didn't say it was unusual, I'm just wondering _why_ in case there's a better way to accomplish the real goal. – D Stanley Apr 23 '14 at 15:37
  • 1
    I am debugging third party code and refactoring it. This is part of the piece that i needed to get the code working in the first place as it was throwing error. – Asdfg Apr 23 '14 at 16:18
  • Does this answer your question? [Cannot convert lambda expression to type 'System.Delegate'](https://stackoverflow.com/questions/9549358/cannot-convert-lambda-expression-to-type-system-delegate) – StayOnTarget Jan 22 '21 at 12:51

5 Answers5

36

Best would be to have the dictionary strongly typed, but if you assign the lambda to a specific lambda (delegate) first, it should work (because the compiler then knows the delegate format):

Action<bool> inp = InProgress => base.InProgress = InProgress;
dict.Add("InProgress", inp);

Or by casting it directly, same effect

dict.Add("InProgress", (Action<bool>)(InProgress => base.InProgress = InProgress));

Of course having such a dictionary format as object is discussable, since you'll have to know the delegate format to be able to use it.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Me.Name
  • 12,259
  • 3
  • 31
  • 48
20

I got this error when I was missing

using System.Data.Entity;
Craig Howard
  • 1,649
  • 15
  • 10
  • This worked for me thanks! Wondering as, in one file this was working and not in another so thought had to be something silly! – Paul Alwin May 29 '18 at 21:23
  • You are awesome! This was it for me ... This is also the first time happening, have been using EF for a while now but never saw this and i expect at least that the suggestion of a Import would arise ... – Dimitri Mar 12 '19 at 08:42
6

Although the solution by @Me.Name is completely valid by itself, there's an additional trick that may come in handy in some situations (it certainly did for me): if you're converting multiple lambdas using this technique, you can factor the cast as a helper method, along the lines of

object myDelegateToObject ( Action<bool> action ) {
    return action; // autocast to `object` superclass, no explicit cast needed
}

and then call it by simply

dict.Add("InProgress", myDelegateToObject(InProgress => base.InProgress = InProgress));

It may save you time later on - if you decide to change to change the signatures, you will have to do so in one place only.

0

I ran into this problem while writing unit tests. I was attempting to mock the behavior of a database to return a new object from a repository instead of actually connecting to a database.

Make sure your object has a usable constructor. You may not be able to successfully instantiate that object the way you want to. Ensure if you using a lambda to point to a constructor that the constructor can be called in the same way in a normal instantiation statement.

i.e.

return x => new FakeObject();

say in the case of

var fake = new FakeObject();

would not work then the lambda will also fail so be careful.

David Hunsicker
  • 1,140
  • 3
  • 14
  • 24
0

If you encounter this error in nopcommerce you need to add import line using System.Linq;

Dinuka Salwathura
  • 924
  • 16
  • 33