如何将Delphi的记录类型高效转换为C语言结构体?
- 内容介绍
- 文章标签
- 相关推荐
本文共计358个文字,预计阅读时间需要2分钟。
当尝试从现有应用程序编写C语言时,以下是一个简化的开头内容:
c#include
int main() { // 程序的主要逻辑将在这里实现 printf(这是一个C语言程序。\n); 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引发异常.
本文共计358个文字,预计阅读时间需要2分钟。
当尝试从现有应用程序编写C语言时,以下是一个简化的开头内容:
c#include
int main() { // 程序的主要逻辑将在这里实现 printf(这是一个C语言程序。\n); 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引发异常.

