是否可以在非托管代码中使用属性?

发布于 2024-09-25 21:58:19 字数 1006 浏览 4 评论 0原文

我有一个非托管的 C++ 应用程序(非托管的含义:不使用任何花哨的 .Net 东西)。我想用一些元信息来扩展它,看起来我可以使用属性的概念。

我实际上想要实现的目标如下。 从这样一个简单的类开始:

class Book
   {
   public:
      ...
   private:
      string m_name;
      string m_author;
      int    m_year;
   };

我想构建可以访问类的“元信息”的功能,并使用它动态地在其上构建逻辑,例如

  • 包含 3 个编辑字段(名称、作者、年份)的
  • 对话框具有 3 列的数据网格
  • 序列化
  • 逻辑将此类映射到具有 3 列的数据库表
  • ...

我最疯狂的梦想是像这样修改此类:

[id="Book"]
class Book
   {
   public:
      ...
   private:
      [id="Name", defaultValue="", maximumLength=100]
      string m_name;

      [id="Author", defaultValue="", maximumLength=100]
      string m_author;

      [id="Year", defaultValue=2000, minimum=1900]
      int    m_year;
   };

然后能够获取此“元”信息来构建对话框,填充数据网格、序列化和反序列化实例……

但是,属性的​​概念是否仅限于 .Net/托管代码?

如果我可以在非托管代码中使用属性,是否可以做这样的事情?什么是一个好的起点? (示例,...)

此外,在其他平台上的其他编译器中是否可以找到相同(或相似)的概念?

我正在使用 Visual Studio 2010,并且如前所述,使用非托管/本机 C++。

I have an unmanaged C++ application (unmanaged meaning: not using anything of the the fancy .Net stuff). I want to extend it with some meta information, and it looks like I could use the concept of attributes.

What I actually try to achieve is the following.
Starting from something a simple class like this:

class Book
   {
   public:
      ...
   private:
      string m_name;
      string m_author;
      int    m_year;
   };

I want to build functionality that can access the 'meta information' of the class and use it to dynamically build logic on it, e.g.

  • a dialog containing 3 edit fields (name, author, year)
  • a data grid with 3 columns
  • serialization logic
  • logic that maps this class to a database table with 3 columns
  • ...

I my wildest dreams I imagine modifying this class like this:

[id="Book"]
class Book
   {
   public:
      ...
   private:
      [id="Name", defaultValue="", maximumLength=100]
      string m_name;

      [id="Author", defaultValue="", maximumLength=100]
      string m_author;

      [id="Year", defaultValue=2000, minimum=1900]
      int    m_year;
   };

And then being able to get this 'meta' information to build up dialogs, filling data grids, serializing and deserializing instances, ...

But, is the concept of attributes limited to .Net/managed code?

And if I could use attributes in unmanaged code, would it be possible to do something like this? And what is a good place to start? (examples, ...)

Also, can the same (or similar) concepts be found in other compilers, on other platforms?

I am using Visual Studio 2010 and, as said before, unmanaged/native C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

又爬满兰若 2024-10-02 21:58:19

Visual C++ 在定义 COM 对象时一度支持类似的属性表示法。我认为支持最终被放弃,因为程序员在想要完全控制时使用 C++ 来实现 COM 实现,而编译器在程序员控制之外神奇地做事与此背道而驰。

OTOH IDL 仍然允许您定义元数据,它会编译为 C++ 源代码以及包含元数据的类型库,并且可以在运行时检索。

Visual C++ for a while supported a similar attribute notation when defining COM objects. I think support was eventually dropped because programmers use C++ for COM implementation when they want complete control, and the compiler doing things magically outside the programmer's control runs counter to that.

OTOH IDL does still allow you to define metadata, it compiles to C++ source code along with a type library which contains the metadata, and it can be retrieved at runtime.

嗫嚅 2024-10-02 21:58:19

不。C++ 没有内省或属性。

查看 Boost Serialization 了解序列化内容,据我所知,对于其他人,您需要手动实现它。

No. C++ does not have introspection or attributes.

Look into Boost Serialization for the serialization stuff, for the others you need to implement it manually, as far as I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文