C#如何获取引用类型在内存中的地址

.Net技术 码拜 9年前 (2015-01-03) 6140次浏览 0个评论
在unsafe代码断中,通过&只能获得值类型在内存中的地址,如MSDN中给出的代码断:
pre class=”brush: csharp”>
class AddressOfOperator
{
static void Main()
{
int number;
unsafe
{
// Assign the address of number to a pointer:
int* p = &number;
// Print the value of *p:
System.Console.WriteLine(“Value at the location pointed to by p: {0:X}”, *p);
// Print the address stored in p:
System.Console.WriteLine(“The address stored in p: {0}”, (int)p);
}
System.Console.ReadKey();
}
}
/pre>
可是引用类型,比如string,声明string类型的指针时会报错:
span style=”color: #FF0000;”>无法获取托管类型(“string”)的地址和大小,或无法声明指向它的指针
请指教引用类型的地址要怎么样才能取到?
多谢~


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#如何获取引用类型在内存中的地址
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!