简体   繁体   English

如何存储有效的静态类类型表,并在C#中动态调用这些类的方法?

[英]How can I store an efficient table of static class types, and dynamically call methods of these classes in C#?

I'm working on a client-server application which transmits a "command" from one endpoint to another. 我正在开发一个客户端服务器应用程序,该应用程序将“命令”从一个端点传输到另一个端点。 I want this command to essentialy be an index into a table of class types which can be dynamically created to perform tasks as the commands come in. 我希望此命令本质上是类类型表的索引,该类类型表可以动态创建以在命令进入时执行任务。

I need to store references to a large number of worker classes (~4096 of them) in the most efficient way possible. 我需要以尽可能最有效的方式存储对大量工作者类(其中约4096个)的引用。

Any advice would be greatly appreciated. 任何建议将不胜感激。

Use a dictionary (Key/Value pair or even just an simple array) of an interface type 使用接口类型的字典(键/值对,甚至只是一个简单的数组)

IMyCommand 我的命令

and when the command index comes in, just invoke the Execute() method on the interface (or whatever you choose). 当命令索引进入时,只需在接口(或您选择的任何接口)上调用Execute()方法。 Of course this requires you instantiate the objects before hand which might not be smart memory wise. 当然,这需要您先实例化对象,这可能不是明智的存储方式。

Otherwise, store the fully qualified type name and use reflection to instantiate that type as IMyCommand and then run Execute() method. 否则,存储完全限定的类型名称,并使用反射将其实例化为IMyCommand,然后运行Execute()方法。

You should create a Dictionary<string, ICommand> , or something similar. 您应该创建一个Dictionary<string, ICommand>或类似的东西。
For more details, please supply more detail. 有关更多详细信息,请提供更多详细信息。

If you need to create a new class instance every time, use a Dictionary<string, Func<ICommand>> . 如果您每次需要创建一个新的类实例,请使用Dictionary<string, Func<ICommand>>

Since you are using static classes, you might want to use a Dictionary<int, Action<T>> or Dictionary<int, Func<T,T>> 由于您使用的是静态类,因此可能要使用Dictionary<int, Action<T>>Dictionary<int, Func<T,T>>

On startup populate the dictionary MyDict[0x0001] = StaticClass.StaticMethod" 启动时,填充字典MyDict[0x0001] = StaticClass.StaticMethod"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以导入一个 static class 作为命名空间来调用它的方法而不在 ZD7EFA19FBE7D3972FDZ5ADB6 中指定 class 名称吗? - Can I import a static class as a namespace to call its methods without specifying the class name in C#? 如何从C#中的不同类在字典中存储不同类型的方法 - How to store different types of methods in a dictionary from different classes in C# Unity C#-如何做到这一点,以便可以通过一个类访问所有其他类,变量和方法? - Unity c# - How can I make it so that I can access all my other classes, variables and methods via a single class? 因为EmguCV对于UWP不可用,我如何从uwp调用c#类/方法 - SInce EmguCV is unavailable for UWP , how can i call c# class/methods from uwp 如何在C#中的另一个类之外调用非静态方法? - How can I call a non-static method out of another class in C#? 调用对话框方法在C#中形成一个静态类 - Call Dialog Methods form a static Class in C# 在C#中编写静态和非静态方法时,如何避免出现“调用不明确……”错误? - How do I avoid 'call is ambiguous…' error when writing static and non-static methods in C#? 如何在C#中使用Namespace.Class动态调用方法? - How can I dynamically call a method using Namespace.Class in C#? 如何在VB代码中调用C#扩展方法 - How can I call C# extension methods in VB code from / where 我可以在类中调用静态类方法吗? - from / where Can i call static class methods in a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM