xml - 可以在 D 中获取变量名吗?

标签 xml serialization d variable-names

我目前正在尝试用 D 编写一个程序,当调用并传递一个对象时,它将将该对象序列化为一个 XML 文档。我想让它像将对象传递给它一样简单,但我不完全确定它可以完成。 示例:

class A
{
    //Constructors and fluff
    ....

    int firstInt;
    int secondInt;
}

.....
A myObj = new A();
XMLSerialize(myObj);

输出为

<A.A>
    <firstInt></firstInt>
    <secondInt></secondInt>
</A.A>

那么,我是否有可能获得对象内部变量的名称,还是必须全部手动完成?

最佳答案

代码胜一千字(有意简化):

import std.stdio;

void print(T)(T input)
    if (is(T == class) || is(T == struct))
{
    foreach (index, member; input.tupleof)
    {
        writefln("%s = %s", __traits(identifier, T.tupleof[index]), member);
    }
}

struct A
{
    int x, y;
}

void main()
{
    print(A(10, 20));
}

关于xml - 可以在 D 中获取变量名吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20274172/

相关文章:

d - 标准D型和GDC(LDC)规范

ruby-on-rails - 如何按属性值搜索

php xml dom getElementById 和 DOMXpath 查询获取元素失败

c# - 意外的转换 Xml 到数据集

PHP反序列化JS序列化的变量字符串

转换帮助 : __asm__ __volatile__

d - 在 D 中等效退出?

java - Android 仅从纵向布局文件夹中获取 xml

sql - 使用 SQL Server 2008 表中的新值更新 Xml 属性

java - Scala - InvalidClassException : no valid constructor