class Node
{
public Point Data;
public Node next;
}
当
Node n=new Node();
Node * pN=&n;
这样会提示错误..不能获取托管类型的地址...如题... 估计没有人会..
{
public Point Data;
public Node next;
}
当
Node n=new Node();
Node * pN=&n;
这样会提示错误..不能获取托管类型的地址...如题... 估计没有人会..
调试欢乐多
第二:貌似只有值类型才能获得地址(Point是值类型,Node应该是个引用类型吧)
引用类型存在于托管堆上,应该不能获取
namespace WindowsApplication6
{
class Node
{
public Point Data;
public Node next;
} public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Node n = new Node();
IntPtr P = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(n);
MessageBox.Show(P.ToString());
n = new Node();
P = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(n);
MessageBox.Show(P.ToString());
n = new Node();
P = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(n);
MessageBox.Show(P.ToString());
n = new Node();
P = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(n);
MessageBox.Show(P.ToString());
n = new Node();
P = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(n);
MessageBox.Show(P.ToString());
}
}
}
在C#中,可以用unsafe代码使用指针。但是,指针只能指向值类型,不能指向引用类型。参考http://msdn.microsoft.com/zh-cn/library/y31yhkeb(v=VS.80).aspx
如果你的类是个轻量型的,可以考虑把该类写成struct,就可以用*操作符直接获取地址。
2.IntPtr类型。
在System.Runtime.InteropServices名称空间里,有一些用于
在托管内存与非托管内存之间互操作的类。Marshal.GetIUnknownForObject()。一般用于调用com对象。比如activex对象。参考http://www.wangchao.net.cn/bbsdetail_577247.html
但是关键看你要获取对象地址的用途是什么。托管对象每次垃圾回收极可能进行内存搬运,地址都会发生改变。