如何将Delphi的记录类型高效转换为C语言的对应结构体?
- 内容介绍
- 文章标签
- 相关推荐
本文共计424个文字,预计阅读时间需要2分钟。
当我尝试从现有应用程序编写C语言时,首先需要确定目标平台和编译环境。接着,我会分析应用程序的功能需求,然后逐步编写代码,实现所需功能。以下是一个简单的示例:
c#include
int main() { int num1, num2, sum;
printf(请输入两个整数:); scanf(%d %d, &num1, &num2);
sum=num1 + num2;
printf(它们的和是:%d\n, sum);
return 0;}
当我尝试从现有应用程序编写C#应用程序但在Delphi中开发时,非常艰难,但管理了一些如何,但现在我遇到了一个问题……
Delphi代码包含以下代码:
type TFruit = record name : string[20]; case isRound : Boolean of // Choose how to map the next section True : (diameter : Single); // Maps to same storage as length False : (length : Single; // Maps to same storage as diameter width : Single); end;
即一个变体记录(内部有case语句),因此也构建了记录及其大小.
另一方面,我试图在C#struct中做同样的事情,但还没有成功,我希望有人能在这里帮助我.
所以,请告诉我是否有任何方法可以在C#中实现这一点.
提前致谢….
我会这样做:
struct Fruit { string name; bool isRound; float diameter; // only valid when isRound is true float length; // only valid when isRound is false float width; // only valid when isRound is false }
更优雅的选项是具有每个struct字段属性的类.如果为了无效的isRound值访问它们,你会安排3个浮点数的属性getter和setter引发异常.
本文共计424个文字,预计阅读时间需要2分钟。
当我尝试从现有应用程序编写C语言时,首先需要确定目标平台和编译环境。接着,我会分析应用程序的功能需求,然后逐步编写代码,实现所需功能。以下是一个简单的示例:
c#include
int main() { int num1, num2, sum;
printf(请输入两个整数:); scanf(%d %d, &num1, &num2);
sum=num1 + num2;
printf(它们的和是:%d\n, sum);
return 0;}
当我尝试从现有应用程序编写C#应用程序但在Delphi中开发时,非常艰难,但管理了一些如何,但现在我遇到了一个问题……
Delphi代码包含以下代码:
type TFruit = record name : string[20]; case isRound : Boolean of // Choose how to map the next section True : (diameter : Single); // Maps to same storage as length False : (length : Single; // Maps to same storage as diameter width : Single); end;
即一个变体记录(内部有case语句),因此也构建了记录及其大小.
另一方面,我试图在C#struct中做同样的事情,但还没有成功,我希望有人能在这里帮助我.
所以,请告诉我是否有任何方法可以在C#中实现这一点.
提前致谢….
我会这样做:
struct Fruit { string name; bool isRound; float diameter; // only valid when isRound is true float length; // only valid when isRound is false float width; // only valid when isRound is false }
更优雅的选项是具有每个struct字段属性的类.如果为了无效的isRound值访问它们,你会安排3个浮点数的属性getter和setter引发异常.

