C# 外部函数数组
好吧,这又是关于我的项目的。我有客户端和服务器。它们相互通信,数据包具有以下结构 [操作码] [长度] [数据]
现在在服务器端,我想从外部 .cs 文件加载每个操作码的功能代码。
所以编译时它看起来像这样
- OPCODES
-- OPCODE1.cs
-- OPCODE2.cs
-- OPCODE3.cs
-- OPCODE4.cs
OPCODES.cfg (has list of all op codes and location for function code)
MyProgram.exe
我该怎么做这样的事情?
Okay so it's about my project yet again. I have client and server. They communicate between each other and packets have following structure [OP CODE] [LENGTH] [DATA]
Now on server-side I wanna load function code for each OPcode from external .cs file.
So when compiled it will look like this
- OPCODES
-- OPCODE1.cs
-- OPCODE2.cs
-- OPCODE3.cs
-- OPCODE4.cs
OPCODES.cfg (has list of all op codes and location for function code)
MyProgram.exe
How would I do something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个字典(假设操作码是一个整数
来执行操作码“7”,
如果您希望它更健壮,您可以这样做:
如果您想从文件加载动作字典,则有点复杂
您可以通过像这样的字典调用静态方法
ConsoleApplicationTest.Program.MyMethod()
字典可以看起来像这样,
并且该方法将像这样被调用
You can create a Dictionary (assuming that the opcode is an integer
to execute opcode "7" you can do
if you want it more robust:
If you want to load the actions Dictionary from a file it is a bit more complicated
You can call the static method
ConsoleApplicationTest.Program.MyMethod()
via a Dictionary like thisThe Dictonary can look like this
and the method will be called like this
您可以使用 case switch 来表示已收到某些数据包。
You can use case switch to signal that some packet received.
看起来您正在寻找比简单的枚举或动作字典更动态的东西。您在问题中提到“OPCODES.cfg(包含所有操作代码和功能代码位置的列表)”。这是否意味着操作码函数不一定与主服务器应用程序一起编译?
如果是这种情况,您可能会考虑某种插件架构,这样您就可以随时添加和删除操作码函数。我推荐 MEF 作为一个很好的起点。
It looks like you're looking for something much more dynamic than a simple enum or dictionary of actions. You mention in your question that "OPCODES.cfg (has list of all op codes and location for function code)". Does this imply that the opcode functions will not necessarily be compiled with the main server application?
If that's the case you'll likely be looking at some kind of plugin architecture, so you can add and remove opcode functions whenever you like. I'd recommend MEF as a good starting point.