将多方法对象传递给另一个类
作为我正在从事的个人项目的一部分,我采用了另一位程序员编写的小型代码库,并尝试根据自己的目的重构它。长话短说,他们有一大段代码是传入的命令解析器。我将代码移动到它自己的类文件中,并将几个代码位移动到该类中它们自己的方法中。我遇到的问题是我无法将两个所需的对象从原始代码块传递到新的解析器类。原始代码部分使用一个名为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AllSocket 方法被声明为
从快速查看代码来看,AllSockets 似乎在类的私有 Shout(..) 方法中使用 - as 方法。因此,为了将其作为函数对象传递,您可以使用 delegate 或 Function: 类型的参数声明 CheckForCommands 方法,
或者使用 Func 而不是委托来声明类似的方法。
The AllSocket method is declared as
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:
or similar with the Func instead of a delegate.
将
object _AllSockets
替换为:您可能应该在 CheckForCommands、Shout 和 Tell 方法签名中替换它。
Replace
object _AllSockets
with:You should probably replace this in CheckForCommands, Shout and Tell method signatures.
AllSockets
是一种方法。我猜您想要获取 AllSockets 方法返回的对象并将其传递到commandParser.CheckForCommands 函数中。尝试类似的方法:
AllSockets
is a method. I'm guessing you want to take the object that theAllSockets
method returns and pass that into thecommandParser.CheckForCommands
function.Try something like: