سؤال

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).

هل كانت مفيدة؟

المحلول

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/

نصائح أخرى

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.

Even most of the normal .Net reflection isn't included in the subset of .Net available to WinRT applications. And I didn't find any reflection-related types in the WinRT documentation. This means that (unless I overlooked something) reflection is simply not exposed by the available APIs.

Although I don't see why it shouldn't be available. The metadata is there, which should be enough.

When looking at the C++-specific functions, there is the function RoGetMetaDataFile(). It seems it should be possible to use it to get the metadata. But it's a native C++ function, not C++/CX. This means it's not easy to use (manual memory management, …) and I doubt it will be allowed in apps that are in the Store.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top