开发者

Is it possible to use attributes in unmanaged code?

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_au开发者_如何学Gothor;
      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++.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜