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

前端 未结 5 1094
广开言路
广开言路 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

    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 inp = InProgress => base.InProgress = InProgress;
    dict.Add("InProgress", inp);
    

    Or by casting it directly, same effect

    dict.Add("InProgress", (Action)(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.

提交回复
热议问题