如何阅读在C#中的.h文件中的常量 [英] how to read constants from .h file in C#

查看:164
本文介绍了如何阅读在C#中的.h文件中的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读在C#中的常量,这些常量在对C .h文件中定义++。谁能告诉我如何在C#中执行此?在C ++的.h文件看起来像:

I want to read the constants in C#, these constants are defined in a .h file for C++. Can anyone tell me how to perform this in C# ? The C++ .h file looks like:

#define MYSTRING1 "6.9.24 (32 bit)"
#define MYSTRING2 "6.8.24 (32 bit)"

我想在C#阅读本?

推荐答案

下面是一个非常简单的答案,你可以用你的.h文件中的每一行提取字符串值。

Here is a really simple answer that you can use on each line of your .h file to extract the string value.

string GetConstVal(string line)
{
  string[] lineParts = string.Split(line, ' ');
  if (lineParts[0] == "#define")
  {
    return lineParts[2];
  }
  return null;
}



所以它返回null的任何时候,你没有一个常数。现在,请记住,它是只得到不断的,不是名称的值,但是你可以很容易地修改代码以返回以及通过输出参数,等等。

So any time it returns null, you don't have a constant. Now keep in mind that it is only getting the value of the constant, not the name, but you can easily modify the code to return that as well via out parameter, etc.

如果您想代表其他数据类型,如整数,等你将不得不想些聪明的,因为在C ++中的宏并不真正算作类型安全的。

If you want to represent other data types, like integers, etc. you will have to think of something clever since macros in C++ don't really count as type safe.

这篇关于如何阅读在C#中的.h文件中的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆