首页 / C# / 将struct数组从C转换为C#的问题
将struct数组从C转换为C#的问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了将struct数组从C转换为C#的问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1910字,纯文字阅读大概需要3分钟。
内容图文
![将struct数组从C转换为C#的问题](/upload/InfoBanner/zyjiaocheng/742/82f2bd35697b4f6988ee8c543a5b5c21.jpg)
我想将此C代码转换为C#:
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
现在我想问:
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 ?
解决方法:
你可以不用另一个类INSIDE,并在另一个类中管理你的默认控制台命令.
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有其他更好的方法来实现他想做的事情.并且可能没有回答他的问题,但我希望在他从功能编程语言转换为纯面向对象语言方面指向正确的方向.
内容总结
以上是互联网集市为您收集整理的将struct数组从C转换为C#的问题全部内容,希望文章能够帮你解决将struct数组从C转换为C#的问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。