Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinRT Reflection (C++/CX)

how can I introspect an object in C++/CX? I known how to get its class name (using IInspectable) but I wasn't able to figure out how to get a list of its properties or how to invoke methods if I have just a name of the method (string). I searched for an answer here and at Google but what I found is related to the .NET layer of WinRT (the System.Reflection namespace doesn't seem to be available in C++/CX).

like image 398
Atamiri Avatar asked Feb 22 '23 21:02

Atamiri


2 Answers

As hinted by svick, you take the class name (retrieved from IInspectable::GetRuntimeClassName), hand it to RoGetMetaDataFile. This returns an IMetaDataImport2. Now call IMetaDataImport2::FindTypeDefByName. This returns a typedef token. Now call IMetaDataImport2::GetTypeDefProps which will give you properties about the type.

From the typedef properties, you can retrieve other information - enumerate the methods/fields if it's an interface/struct (or enum), find the type of the runtime class (if it's an interface or a class), etc.

like image 59
ReinstateMonica Larry Osterman Avatar answered Feb 27 '23 02:02

ReinstateMonica Larry Osterman


C++ doesn't provide any specific APIs to reflect on WinRT types, these types are fully defined in CX compliant metadata files and you can use the CLR native metadata APIs to read their definition. There is a snippet at

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/211ef583-db11-4e55-926b-6d9ab53dbdb4/ccx-reflection

James McNellis released a full C++ library for CX reflection last year

http://seaplusplus.com/2012/04/26/cxxreflect-native-reflection-for-the-windows-runtime/

like image 20
CS Pei Avatar answered Feb 27 '23 01:02

CS Pei