本文介绍了如何获得原始内存指针托管类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何找到一个原始指针的管理的类在C#中,并希望它在内存中的原始大小?显然,这不是由CLR允许的 - 更准确地说,是严格禁止的,因为托管类的非托管表示应该永远永远也不能为稳定性和安全工作的原因 - 所以我在寻找一个黑客。我不是在寻找序列化 - 我的确需要管理类的垃圾,因为它是在原始内存代表

How do I find a raw pointer to a managed class in C#, and, hopefully, it's raw size in memory? Obviously, this is not allowed by CLR - more precisely, strictly prohibited, as unmanaged representation of managed classes should never, ever be worked with for both stability and safe reasons - so I'm looking for a hack. I'm not looking for serializing - I do actually need a dump of managed class as it is represented in raw memory.

更确切地说,我寻找类似功能 getObjectPtr 下面的例子:

More precisely, I'm looking for something like function getObjectPtr in the following example:

IntPtr getObjectPtr(Object managedClass) {...}

void main() {
    var test=new TestClass();
    IntPtr* ptr_to_test=getObjectPtr(test);
    Console.WriteLine(ptr_to_test.ToString());
}



在此先感谢!

Thanks in advance!

推荐答案

嘿这是你想要什么:

GCHandle gcHandle = GCHandle.Alloc(yourObject,GCHandleType.WeakTrackResurrection);
IntPtr thePointer = GCHandle.ToIntPtr(gcHandle);  

这篇关于如何获得原始内存指针托管类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 11:28