我试图从C#中的int指针获取低内存技巧的内存地址。但是,当我检查输出时,它似乎是打印int的值,而不是它的内存地址。以下是我的代码:
using (XmlWriter writer = XmlWriter.Create(newBuilder))
{
writer.WriteStartDocument();
writer.WriteStartElement("Data");
GCHandle handle = GCHandle.Alloc(Nodes, GCHandleType.Normal);
//fixed(int * p = &Nodes[0].value) {
for (int i = 0; i < Nodes.Count; i++)
{
writer.WriteStartElement("Node");
writer.WriteStartElement("Value");
writer.WriteValue(this.Nodes[i].Value);
writer.WriteEndElement();
fixed (int* name = &Nodes[i].value)
{
writer.WriteStartElement("Name");
writer.WriteValue(name->ToString());
writer.WriteEndElement();
}
if (Nodes[i].Parent1 != null)
{
fixed (int* parent1 = &Nodes[i].Parent1.value)
{
writer.WriteStartElement("Parent1");
writer.WriteValue(parent1->ToString());
writer.WriteEndElement();
}
}
if (Nodes[i].Parent2 != null) {
fixed (int* parent2 = &Nodes[i].Parent2.value)
{
writer.WriteStartElement("Parent2");
writer.WriteValue(parent2->ToString());
writer.WriteEndElement();
}
}
writer.WriteEndElement();
}
//}
handle.Free();
writer.WriteEndElement();
writer.WriteEndDocument();
//XmlSerializer serializer = new XmlSerializer(typeof(List<Node>));
//serializer.Serialize(writer, this.Nodes);
}
知道为什么会这样做吗?
由于
答案 0 :(得分:2)
您的代码会写入整数的值,因为您dereference the pointer并在整数值上调用ToString Method。
答案 1 :(得分:1)
试试((IntPtr)name).ToString()
。 name->ToString()
调用ToString
指向的int
上的name
方法。