// Get number of generic parameters on a given type. // Eg, for 'Foo<t, u>', returns 2. // for 'Foo', returns 0. public int CountGenericParams(int typeToken) { // This may fail in pre V2.0 debuggees. //Guid IID_IMetadataImport2 = new Guid("FCE5EFA0-8BBA-4f8e-A036-8F2022B08466"); if (!(m_importer is IMetadataImport2)) { return(0); // this means we're pre v2.0 debugees. } IMetadataImport2 importer2 = (m_importer as IMetadataImport2); Debug.Assert(importer2 != null); int dummy; uint dummy2; IntPtr hEnum = IntPtr.Zero; int count; importer2.EnumGenericParams(ref hEnum, typeToken, out dummy, 1, out dummy2); try { m_importer.CountEnum(hEnum, out count); } finally { if (hEnum != IntPtr.Zero) { importer2.CloseEnum(hEnum); } } return(count); }
// Function translated directly from cor.h but never tested; included here in case someone wants to use it in future /* internal static uint CorSigUncompressElementType(// return number of bytes of that compressed data occupied in pData * IntPtr pData, // [IN] compressed data * out CorElementType pElementType) // [OUT] the expanded *pData * { * unsafe * { * byte *pBytes = (byte*)pData; * pElementType = (CorElementType)(*pBytes & 0x7f); * return 1; * } * }*/ static internal string[] GetGenericArgumentNames(IMetadataImport importer, int typeOrMethodToken) { IMetadataImport2 importer2 = (importer as IMetadataImport2); if (importer2 == null) { return(new string[0]); // this means we're pre v2.0 debugees. } Debug.Assert(importer2 != null); string[] genargs = null; IntPtr hEnum = IntPtr.Zero; try{ int i = 0; do { uint nOut; int genTypeToken; importer2.EnumGenericParams(ref hEnum, typeOrMethodToken, out genTypeToken, 1, out nOut); if (genargs == null) { int count; importer.CountEnum(hEnum, out count); genargs = new string[count]; } if (nOut == 0) { break; } Debug.Assert(nOut == 1); if (nOut == 1) { uint genIndex; int genFlags, ptkOwner, ptkKind; ulong genArgNameSize; importer2.GetGenericParamProps(genTypeToken, out genIndex, out genFlags, out ptkOwner, out ptkKind, null, 0, out genArgNameSize); StringBuilder genArgName = new StringBuilder((int)genArgNameSize); importer2.GetGenericParamProps(genTypeToken, out genIndex, out genFlags, out ptkOwner, out ptkKind, genArgName, (ulong)genArgName.Capacity, out genArgNameSize); genargs[i] = genArgName.ToString(); } ++i; } while(i < genargs.Length); } finally { if (hEnum != IntPtr.Zero) { importer2.CloseEnum(hEnum); } } return(genargs); }