0

所以我想知道为什么这段代码给我带来了问题?

this.adminList.Invoke((Delegate)(() => this.adminList.Items.Add((object)this.arg2)));

它给我的错误是:

错误 1 ​​无法将 lambda 表达式转换为类型“System.Delegate”,因为它不是委托类型

有谁知道为什么会这样?哦,顺便说一句,当我尝试这样做时,adminList.Items.Add(arg2); 它给了我一个错误,当我尝试时,“交叉线程未在创建它的线程上使用”但无论如何,arg2就像“用户名”,而 adminList 是一个列表框. 这是c#。感谢您的帮助和时间 :D PS 如果您能修改此内容将非常有帮助,谢谢!

PSS:根据要求提供更多代码

            if (this.str.StartsWith("!addadmin") || this.str.StartsWith("!admin"))
            {
                if (Enumerable.Contains<char>((IEnumerable<char>)this.str, this.space))
                {
                    if (this.arg2 == this.names[m.GetInt(0U)])
                        con.Send("say", new object[1]
        {
          (object) (this.names[m.GetInt(0U)].ToUpper() + ": You can't make yourself an admin.")
        });
                    else if (this.mods.Contains((object)this.names[m.GetInt(0U)]))
                    {
                        if (this.names.ContainsValue(this.arg2))
                        {
                            if (!this.adminList.Items.Contains((object)this.arg2))
                            {
                                if (this.adminList.InvokeRequired)
                                    this.adminList.Invoke((Delegate)(() => this.adminList.Items.Add((object)this.arg2)));
                                con.Send("say", new object[1]
            {
              (object) (this.names[m.GetInt(0U)].ToUpper() + ": " + this.arg2.ToUpper() + " is added as an admin.")
            });
                            }
                            else
                                con.Send("say", new object[1]
            {
              (object) (this.names[m.GetInt(0U)].ToUpper() + ": " + this.arg2.ToUpper() + " is already an admin...")
            });
                        }
                        else
                            con.Send("say", new object[1]
          {
            (object) (this.names[m.GetInt(0U)].ToUpper() + ": " + this.arg2.ToUpper() + " doesn't exist in this world.")
          });
                    }
                    else
                        con.Send("say", new object[1]
        {
          (object) (this.names[m.GetInt(0U)].ToUpper() + ": You need to be a mod or the owner to use \"" + this.arg1 + "\"")
        });
                }
                else
                    con.Send("say", new object[1]
      {
        (object) ( this.names[m.GetInt(0U)].ToUpper() + ": Use it like: \"" + this.arg1 + " USERNAME\"")
      });
            }

names 是存储用户名的字典,mods 是比管理员拥有更多权力的 ppl 列表,m 是 PlayerIOClient.Message,arg1 是说的第一个单词,str 是用户说话时文本的字符串

就像我之前说的,adminList 是一个列表框

4

1 回答 1

5

无法将 lambda 表达式推断为特定的委托类型(因为可能有多个具有匹配签名的委托),因此您需要显式指定委托类型。Delegate不是具体的委托类型,它只是所有委托的基类,所以你不能使用它。

在您的情况下,您可以使用Action委托:

this.adminList.Invoke(new Action(() => this.adminList.Items.Add(this.arg2)));
于 2013-11-09T01:29:16.527 回答