将多方法对象传递给另一个类

发布于 2025-01-03 23:36:38 字数 877 浏览 3 评论 0原文

作为我正在从事的个人项目的一部分,我采用了另一位程序员编写的小型代码库,并尝试根据自己的目的重构它。长话短说,他们有一大段代码是传入的命令解析器。我将代码移动到它自己的类文件中,并将几个代码位移动到该类中它们自己的方法中。我遇到的问题是我无法将两个所需的对象从原始代码块传递到新的解析器类。原始代码部分使用一个名为 AllSockets 的多方法对象。我似乎无法弄清楚如何将此对象传递给新的类方法而不出现错误

错误 2 参数 2:无法从“方法组”转换为“对象”

调用代码行:

bool wasValidCommand = (commandParser.CheckForCommands(thisConnection, AllSockets, characterPath, helpFilePath));

调用的方法:

public bool CheckForCommands(ConnectionInfo _connected, object _AllSockets, string characterPath, string helpFilePath)

我已在此处发布托管 Server.cs 类:http://codepad.org/1kRHA1nk

我试图将对象传递给的新类在这里:http://codepad.org/oONRaEtt

警告:代码现在确实被拼凑在一起,变量命名是一场噩梦。如果更容易阅读,我可以粘贴特定部分。任何帮助将不胜感激。

As part of a personal project I am working on I took a small codebase written by another programmer and was trying to refactor it to my own purposes. Long story short, they had this huge section of code that was an incoming command parser. I moved the code to it's own class file and moved several code bits to their own methods in that class. The problem I'm encountering is that I can't pass two needed objects from the original block of code to the new parser class. The orginal code section uses an mutliple-method object called AllSockets. I can't seem to figure out how to pass this object to the new class method without getting the error

Error 2 Argument 2: cannot convert from 'method group' to 'object'

The invoking line of code:

bool wasValidCommand = (commandParser.CheckForCommands(thisConnection, AllSockets, characterPath, helpFilePath));

The method invoked:

public bool CheckForCommands(ConnectionInfo _connected, object _AllSockets, string characterPath, string helpFilePath)

I've posted the hosting Server.cs class here: http://codepad.org/1kRHA1nk

The new class I am trying to pass the object to is here: http://codepad.org/oONRaEtt

Warning: The code is really hacked together right now and variable naming is a nightmare. I can paste the specific sections if that would be easier to read. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以歌曲疗慰 2025-01-10 23:36:38

AllSocket 方法被声明为

public object AllSockets(string tempFlag, object objectOne, object object_Two, object object_Three)

从快速查看代码来看,AllSockets 似乎在类的私有 Shout(..) 方法中使用 - as 方法。因此,为了将其作为函数对象传递,您可以使用 delegate 或 Function: 类型的参数声明 CheckForCommands 方法,

delegate object AllSocketDelegate (string tempFlag, object objectOne, object object_Two, object object_Three); 
public bool CheckForCommands(ConnectionInfo _connected, AllSocketDelegate _AllSockets, string characterPath, string helpFilePath)

或者使用 Func 而不是委托来声明类似的方法。

The AllSocket method is declared as

public object AllSockets(string tempFlag, object objectOne, object object_Two, object object_Three)

From a quick look over the code, it appears AllSockets is used in the private Shout(..) method of the class - as method. So in order to pass this as a function object, you may declare the CheckForCommands method with a parameter of type delegate or Function:

delegate object AllSocketDelegate (string tempFlag, object objectOne, object object_Two, object object_Three); 
public bool CheckForCommands(ConnectionInfo _connected, AllSocketDelegate _AllSockets, string characterPath, string helpFilePath)

or similar with the Func instead of a delegate.

笑红尘 2025-01-10 23:36:38

object _AllSockets 替换为:

Func<string, object, object, object, object>_AllSockets

您可能应该在 CheckForCommands、Shout 和 Tell 方法签名中替换它。

Replace object _AllSockets with:

Func<string, object, object, object, object>_AllSockets

You should probably replace this in CheckForCommands, Shout and Tell method signatures.

篱下浅笙歌 2025-01-10 23:36:38

AllSockets 是一种方法。我猜您想要获取 AllSockets 方法返回的对象并将其传递到commandParser.CheckForCommands 函数中。

尝试类似的方法:

object allSockets = AllSockets(...);
bool wasValidCommand = (commandParser.CheckForCommands(thisConnection, allSockets, characterPath, helpFilePath));

AllSockets is a method. I'm guessing you want to take the object that the AllSockets method returns and pass that into the commandParser.CheckForCommands function.

Try something like:

object allSockets = AllSockets(...);
bool wasValidCommand = (commandParser.CheckForCommands(thisConnection, allSockets, characterPath, helpFilePath));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文