<legend id='mTHpW'><style id='mTHpW'><dir id='mTHpW'><q id='mTHpW'></q></dir></style></legend>
      <i id='mTHpW'><tr id='mTHpW'><dt id='mTHpW'><q id='mTHpW'><span id='mTHpW'><b id='mTHpW'><form id='mTHpW'><ins id='mTHpW'></ins><ul id='mTHpW'></ul><sub id='mTHpW'></sub></form><legend id='mTHpW'></legend><bdo id='mTHpW'><pre id='mTHpW'><center id='mTHpW'></center></pre></bdo></b><th id='mTHpW'></th></span></q></dt></tr></i><div id='mTHpW'><tfoot id='mTHpW'></tfoot><dl id='mTHpW'><fieldset id='mTHpW'></fieldset></dl></div>

        <small id='mTHpW'></small><noframes id='mTHpW'>

        <tfoot id='mTHpW'></tfoot>
        • <bdo id='mTHpW'></bdo><ul id='mTHpW'></ul>
      1. 无法将 lambda 表达式转换为类型“对象",因为它不是委托类型

        Cannot convert lambda expression to type #39;object#39; because it is not a delegate type(无法将 lambda 表达式转换为类型“对象,因为它不是委托类型)

        • <small id='URCn4'></small><noframes id='URCn4'>

            <tbody id='URCn4'></tbody>
        • <legend id='URCn4'><style id='URCn4'><dir id='URCn4'><q id='URCn4'></q></dir></style></legend>
              <tfoot id='URCn4'></tfoot>
              <i id='URCn4'><tr id='URCn4'><dt id='URCn4'><q id='URCn4'><span id='URCn4'><b id='URCn4'><form id='URCn4'><ins id='URCn4'></ins><ul id='URCn4'></ul><sub id='URCn4'></sub></form><legend id='URCn4'></legend><bdo id='URCn4'><pre id='URCn4'><center id='URCn4'></center></pre></bdo></b><th id='URCn4'></th></span></q></dt></tr></i><div id='URCn4'><tfoot id='URCn4'></tfoot><dl id='URCn4'><fieldset id='URCn4'></fieldset></dl></div>

                  <bdo id='URCn4'></bdo><ul id='URCn4'></ul>
                  本文介绍了无法将 lambda 表达式转换为类型“对象",因为它不是委托类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个基类,它的 bool 属性如下所示:

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

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

                  我正在从它继承另一个类并尝试将 InProgress 作为代表添加到字典中.但它给我一个错误.这就是我的派生类的样子:

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

                  这是我得到的错误:

                  无法将 lambda 表达式转换为类型对象",因为它不是委托类型

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

                  我在这里做错了什么?

                  推荐答案

                  最好是强类型的字典,但是如果你先将 lambda 分配给特定的 lambda(委托),它应该可以工作(因为编译器然后知道委托格式):

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

                  或者直接施法,效果一样

                  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.

                  这篇关于无法将 lambda 表达式转换为类型“对象",因为它不是委托类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                    <bdo id='m3bko'></bdo><ul id='m3bko'></ul>

                    1. <tfoot id='m3bko'></tfoot>

                        <tbody id='m3bko'></tbody>
                      <i id='m3bko'><tr id='m3bko'><dt id='m3bko'><q id='m3bko'><span id='m3bko'><b id='m3bko'><form id='m3bko'><ins id='m3bko'></ins><ul id='m3bko'></ul><sub id='m3bko'></sub></form><legend id='m3bko'></legend><bdo id='m3bko'><pre id='m3bko'><center id='m3bko'></center></pre></bdo></b><th id='m3bko'></th></span></q></dt></tr></i><div id='m3bko'><tfoot id='m3bko'></tfoot><dl id='m3bko'><fieldset id='m3bko'></fieldset></dl></div>

                      <small id='m3bko'></small><noframes id='m3bko'>

                          <legend id='m3bko'><style id='m3bko'><dir id='m3bko'><q id='m3bko'></q></dir></style></legend>