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

前端 未结 5 1097
广开言路
广开言路 2020-12-18 17:25

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

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


        
5条回答
  •  情歌与酒
    2020-12-18 18:15

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

提交回复
热议问题