中国开发网: 论坛: 程序员情感CBD: 贴子 251072
老玉米
holly,看看这个程序里面的typeArgs ,这段我从网上找的程序肯定是正确的,我测试过。
bool CProfilerCallback::ClassName(ClassID classId, LPWSTR wszClass )
{
ClassID parentClassId;
ModuleID moduleId;
mdTypeDef typeDef;
bool bIsArray = false;
ClassID* typeArgs = NULL;
ULONG32 cNumTypeArgs = 16;

// init the string to blank
wszClass[0] = 0;

// Get the information about the class
HRESULT hr = E_FAIL;
// occasionally this call seems to fail when the classID looks ok from inside of movedreferences....
// Since it is possible for the __COMObject types classes (interop) to receive different classId values for the same class
// perhaps we are falling afoul of that since interop requires a different EE class for each application domain so the
// user can potentially create a different v-table for each one of them. The profiler doesn't provide a way to get this appdomain however...
try
{
if(m_pICorProfilerInfo2.p == NULL)
{
hr = m_pICorProfilerInfo->GetClassIDInfo( classId, &moduleId, &typeDef );
}
else
{
typeArgs = new ClassID[cNumTypeArgs];
hr = m_pICorProfilerInfo2->GetClassIDInfo2( classId,
&moduleId,
&typeDef,
&parentClassId,
cNumTypeArgs,
&cNumTypeArgs,
typeArgs);
}
}
catch(...)
{
LogEntry("**** ICorProfilerInfo::GetClassIDInfo failed with classId 0x%08X\r\n",classId);
return false;
}

if ( FAILED(hr) || typeDef == 0)
{
ClassID baseClassId = 0;
CorElementType type;
ULONG cRank = 0;
// might be an array in which case we need to translate the classID to the base classID of the array
hr = m_pICorProfilerInfo->IsArrayClass(classId,&type,&baseClassId,&cRank);
if(FAILED (hr) || hr == S_FALSE)
return false;
else
{
// use the base class of the array
hr = m_pICorProfilerInfo->GetClassIDInfo( baseClassId, &moduleId,
&typeDef );
if(FAILED (hr))
return false;
else
{
// update the classId
classId = baseClassId;
bIsArray = true;
}
}
}


IMetaDataImport * pIMetaDataImport = 0;
// go get the information about the module this class belongs to
hr = m_pICorProfilerInfo->GetModuleMetaData( moduleId, ofRead,
IID_IMetaDataImport,
(LPUNKNOWN *)&pIMetaDataImport );
if ( FAILED(hr) )
return false;

if ( !pIMetaDataImport )
return false;

wchar_t wszTypeDef[NAME_BUFFER_SIZE];
DWORD cchTypeDef = sizeof(wszTypeDef)/sizeof(wszTypeDef[0]);
// get the properties for the typedef for the class
hr = pIMetaDataImport->GetTypeDefProps( typeDef, wszTypeDef, cchTypeDef,
&cchTypeDef, 0, 0 );
if ( FAILED(hr) )
return false;

wcscpy_s( wszClass,1024, wszTypeDef );

// get generic type param names
if(typeArgs != NULL)
{
if(cNumTypeArgs>0)
wcscat_s(wszClass,1024,L"<");
for(ULONG32 i=0;i<cNumTypeArgs;i++)
{
ClassName(typeArgs[i],wszTypeDef);
wcscat_s(wszClass,1024,wszTypeDef);
wcscat_s(wszClass,1024,L",");
}
if(cNumTypeArgs>0)
wcscat_s(wszClass,1024,L">");
}
// add on array identifier if was an array instance
if(bIsArray)
wcscat_s(wszClass,1024,L"[]");

delete [] typeArgs;

// make sure we give back the metadata import instance
pIMetaDataImport->Release();
return true;
}
民主不同于专制,不需要强加于人。--- 賴斯

大道之行也,天下为公,选贤与能,讲信修睦。故人不独亲其亲,不独子其子,使老有所终,壮有所用,幼有所长,矜、寡、孤、独、废疾者,皆有所养。男有分,女有归。货恶其弃於地也,不必藏於己;力恶其不出於身也,不必为己。是故,谋闭而不兴,盗窃乱贼而不作,故外户而不闭,是谓大同。--《礼运·大同篇》

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录