Dan*_*tin 2 xml serialization d variable-names
我目前正在尝试在D中编写一个程序,当调用并传递一个对象时,它会将对象序列化为XML文档.我想让它像传递对象一样简单,但我不能完全确定它可以完成.例:
class A
{
//Constructors and fluff
....
int firstInt;
int secondInt;
}
.....
A myObj = new A();
XMLSerialize(myObj);
Run Code Online (Sandbox Code Playgroud)
输出将是
<A.A>
<firstInt></firstInt>
<secondInt></secondInt>
</A.A>
Run Code Online (Sandbox Code Playgroud)
那么,我是否有可能在对象内部获取变量的名称,或者是否必须手动完成?
代码值千言万语(故意简化):
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));
}
Run Code Online (Sandbox Code Playgroud)