将 #defines 从 .h 文件移植到 C# 应用程序的最佳实践是什么?
我正在将应用程序从 C++ 转换为 C#。 C++ 应用程序有一个定义文件 .h,其中包含超过 500 个 #define 指令。我想在 Definition.cs 文件中表示这些数据。 执行此操作的最佳方法是什么?
定义的示例:
//Digital ouputs
#define FLAGLEDCOOL "BIT12032"
#define FLAGLEDLASERINTLK "BIT12036"
#define FLAGLEDLASERSTANBDY "BIT12038"
...
//Digital inputs
#define FLAGSTATUSINTLKRELAY "BIT11535"
#define FLAGSTATUSEMERGRELAY "BIT11533"
#define FLAGSTATUSKVMRELAY "BIT11531"
...
#defines 被分组,因此这让我想到使用属性,例如:
public class DigitalOuputs
{
public static string FLAGLEDCOOL { get; }
public static string FLAGLEDLASERINTLK { get; }
public static string FLAGLEDLASERSTANBDY { get; }
...
}
public class DigitalInputs
{
public static string FLAGSTATUSINTLKRELAY { get; }
public static string FLAGSTATUSEMERGRELAY { get; }
public static string FLAGSTATUSKVMRELAY { get; }
...
}
尽管我必须在 a 中设置默认值构造函数,我试图避免,它们应该是只读的。
谢谢。
I am converting an application from C++ to C#. The C++ application has a defines file .h with over 500 #define directives. I would like to represent this data in a Definition.cs file. What is the best way to do this?
An example of the defines:
//Digital ouputs
#define FLAGLEDCOOL "BIT12032"
#define FLAGLEDLASERINTLK "BIT12036"
#define FLAGLEDLASERSTANBDY "BIT12038"
...
//Digital inputs
#define FLAGSTATUSINTLKRELAY "BIT11535"
#define FLAGSTATUSEMERGRELAY "BIT11533"
#define FLAGSTATUSKVMRELAY "BIT11531"
...
The #defines are grouped so this make me think of using properties, such as:
public class DigitalOuputs
{
public static string FLAGLEDCOOL { get; }
public static string FLAGLEDLASERINTLK { get; }
public static string FLAGLEDLASERSTANBDY { get; }
...
}
public class DigitalInputs
{
public static string FLAGSTATUSINTLKRELAY { get; }
public static string FLAGSTATUSEMERGRELAY { get; }
public static string FLAGSTATUSKVMRELAY { get; }
...
}
Although I would have to set the default value in a constructor, which im trying to avoid, and they should be read-only.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将它们声明为 const - a const 也是隐式静态的:
Just declare them as consts - a const is implicitly a static as well: