typedef struct consoleCommand_s { char* cmd ; voID (*function)() ;} consoleCommand_t ;static consoleCommand_t commands[] = { {"clIEntList",&CG__ClIEntList},{"say",&CG_Say},{"nick",&CG_Nick},{"logout",&CG_logout}} ;// e.g.static voID CG_logout(voID){ // logout}
我最接近的是:
public class Class1 { // public delegate int Calculate (int value1,int value2); public delegate voID fnCommand_t(); public class consoleCommand_t { public string strCommandname; public fnCommand_t fnCommand; public consoleCommand_t(string x,fnCommand_t y) { this.strCommandname = x; this.fnCommand = y; } // End Constructor } // End Class consoleCommand_t public static voID Nick() { Console.Writeline("Changing Nick"); } // End Sub public static voID logout() { Console.Writeline("Logging out"); } // End Sub // string[] names = new string[] {"Matt","Joanne","Robert"}; public consoleCommand_t[] commands = new consoleCommand_t[] { new consoleCommand_t("Nick",Nick),new consoleCommand_t("logout",logout) }; } // End Class Class1
现在我想问:
解决方法 你可以不用另一个类INSIDE,并在另一个类中管理你的默认控制台命令.A) Why does Nick & logout need to be static when all the rest is not ?
B) Is there no C-like way to initialize the commands array,that is,without new ?
public delegate voID ConsoleCmd();public class DefaultCommands{ public static voID Nick() { Console.Writeline("I'm Nick!"); } public static voID logout() { Console.Writeline("You're Fired!"); }}public class Console{ private Dictionary<string,ConsoleCmd> mCommands; public Console() { mCommands = new Dictionary<string,ConsoleCmd>(); mCommands.Add("Nick",DefaultCommands.Nick); mCommands.Add("logout",DefaultCommands.logout); }}
访问您的命令非常简单:
ConsoleCmd command = mCommands["Nick"];command();
编辑:未能提到这一点.这是指OP有其他更好的方法来实现他想做的事情.并且可能没有回答他的问题,但我希望在他从功能编程语言转换为纯面向对象语言方面指向正确的方向.
@H_502_0@ 总结以上是内存溢出为你收集整理的将struct数组从C转换为C#的问题全部内容,希望文章能够帮你解决将struct数组从C转换为C#的问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)