C# 外部函数数组

发布于 2024-10-18 15:18:29 字数 322 浏览 1 评论 0原文

好吧,这又是关于我的项目的。我有客户端和服务器。它们相互通信,数据包具有以下结构 [操作码] [长度] [数据]

现在在服务器端,我想从外部 .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 技术交流群。

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

发布评论

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

评论(3

天邊彩虹 2024-10-25 15:18:29

您可以创建一个字典(假设操作码是一个整数

        Dictionary<int, Action> actions = new Dictionary<int, Action>();
        actions[7] = () => Console.WriteLine("hello World");

来执行操作码“7”,

        actions[7]();

如果您希望它更健壮,您可以这样做:

        Action action;
        if (actions.TryGetValue(7, out action))
            action();
        // else illegal opcode

如果您想从文件加载动作字典,则有点复杂
您可以通过像这样的字典调用静态方法 ConsoleApplicationTest.Program.MyMethod()

namespace ConsoleApplicationTest
{
public class Program
{
    public static void MyMethod()
    {
        Console.WriteLine("MyMethod called");
    }
 } 
 }

字典可以看起来像这样,

        Dictionary<int, MethodInfo> dynamicActions = new Dictionary<int, MethodInfo>();

        dynamicActions[7] = Assembly.GetExecutingAssembly().GetType("ConsoleApplicationTest.Program").GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Static);

并且该方法将像这样被调用

        MethodInfo method;
        if (dynamicActions.TryGetValue(7, out method))
            method.Invoke(null, new object[0]);

You can create a Dictionary (assuming that the opcode is an integer

        Dictionary<int, Action> actions = new Dictionary<int, Action>();
        actions[7] = () => Console.WriteLine("hello World");

to execute opcode "7" you can do

        actions[7]();

if you want it more robust:

        Action action;
        if (actions.TryGetValue(7, out action))
            action();
        // else illegal opcode

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 this

namespace ConsoleApplicationTest
{
public class Program
{
    public static void MyMethod()
    {
        Console.WriteLine("MyMethod called");
    }
 } 
 }

The Dictonary can look like this

        Dictionary<int, MethodInfo> dynamicActions = new Dictionary<int, MethodInfo>();

        dynamicActions[7] = Assembly.GetExecutingAssembly().GetType("ConsoleApplicationTest.Program").GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Static);

and the method will be called like this

        MethodInfo method;
        if (dynamicActions.TryGetValue(7, out method))
            method.Invoke(null, new object[0]);
不羁少年 2024-10-25 15:18:29

您可以使用 case switch 来表示已收到某些数据包。

switch(packet.OpCode)
{
   case 1:
   OnHelloPacket();
   break;
   case 2:
   OnInfoPaket();
   break;
   //etc
}

You can use case switch to signal that some packet received.

switch(packet.OpCode)
{
   case 1:
   OnHelloPacket();
   break;
   case 2:
   OnInfoPaket();
   break;
   //etc
}
朦胧时间 2024-10-25 15:18:29

看起来您正在寻找比简单的枚举或动作字典更动态的东西。您在问题中提到“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.

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