读取的字符串实习生池的内容 [英] Read the content of the string intern pool
问题描述
我想枚举在字符串实习生池的弦
这是说,我想要得到的字符串
,使得所有实例取值
的列表:
That is to say, I want to get the list of all the instances s
of string
such that:
string.IsInterned(s) != null
请问任何人都知道,如果它是可能的吗?
Does anyone know if it's possible?
推荐答案
的感谢@HansPassant的建议,我设法字符串列表文字在装配中。这是非常接近我本来想。的
您需要使用阅读汇编元数据和枚举用户的字符串。
You need to use read assembly meta-data, and enumerate user-strings. This can be done with these three methods of IMetaDataImport
:
[ComImport, Guid("7DAC8207-D3AE-4C75-9B67-92801A497D44")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMetaDataImport
{
void CloseEnum(IntPtr hEnum);
uint GetUserString(uint stk, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] char[] szString, uint cchString, out uint pchString);
uint EnumUserStrings(ref IntPtr phEnum, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]uint[] rStrings, uint cmax, out uint pcStrings);
// interface also contains 62 irrelevant methods
}
要获得 IMetaDataImport
实例,你需要得到一个 IMetaDataDispenser
:
To get the instance of IMetaDataImport
, you need to get a IMetaDataDispenser
:
[ComImport, Guid("809C652E-7396-11D2-9771-00A0C9B4D50C")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[CoClass(typeof(CorMetaDataDispenser))]
interface IMetaDataDispenser
{
uint OpenScope([MarshalAs(UnmanagedType.LPWStr)]string szScope, uint dwOpenFlags, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppIUnk);
// interface also contains 2 irrelevant methods
}
[ComImport, Guid("E5CB7A31-7512-11D2-89CE-0080C792E5D8")]
class CorMetaDataDispenser
{
}
下面是怎么一回事:
var dispenser = new IMetaDataDispenser();
var metaDataImportGuid = new Guid("7DAC8207-D3AE-4C75-9B67-92801A497D44");
object scope;
var hr = dispenser.OpenScope(location, 0, ref metaDataImportGuid, out scope);
metaDataImport = (IMetaDataImport)scope;
其中,位置
是路径组件文件。
之后,调用 EnumUserStrings()
和 GetUserString()
是简单明了。
After that, calling EnumUserStrings()
and GetUserString()
is straighforward.
Here is a blog post with more detail, and a demo project on GitHub.
这篇关于读取的字符串实习生池的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!