问题
There is something called projected types in WinRT. For example, in metadata, the IXamlType.UnderlyingType
is defined as:
TypeName UnderlyingType { get; }
However when used in a C# app, it changes as follows:
Type UnderlyingType { get; }
My question is - is there any documentation regarding the rules, API, attributes used for such mappings?
回答1:
That's correct, the language projection built into the CLR maps WinRT types to CLR types automatically. Documentation is hard to come by, especially right now when this is still beta material. But there's an excellent white paper available that describes some aspects of the CLR projection. The download is (currently) available here (Note: Word .docx file)
回答2:
When windows metadata is created using the low level authoring tools (MIDL with the /winrt switch and MDMERGE), any places in the assembly that would normally use a typedef, the typedef is replaced with typerefs which point inside the same assembly.
That allows the CLR to rewrite the contents of the winmd file replacing the windows runtime type with a corresponding CLR type. The simplest example of this is the Windows.Foundation.Uri type is replaced with System.Uri to C# applications. The CLR knows internally how to map between W.F.Uri and S.Uri and it automatically does this translation for you.
All of this is handled automagically by the system, there are rules for it, but I don't believe that the process is developer controllable - I believe that the type mappings are burned into the CLR implementation.
回答3:
This is the link I was talking about which is a video on Channel 9 http://channel9.msdn.com/Events/BUILD/BUILD2011/PLAT-874T Please note that this is a video of the Build conference which is based on the Developer Preview. I can't predict how much of this info has changed with the Consumer Preview.
I agree that there should be documentation about how this is working. Hopefully they will update the documentation on MSDN soon.
回答4:
They say that the source code is the best documentation. In this case it seems to be the only documentation. Here are my findings from spelunking through the .NET source.
The list of metadata projections is encoded in the .NET source using a macro iterator.
This header is included in various places where it is transformed into data structures for the task at hand. The most accessible place to us that I've found is in the WinMD metadata importer and its adapter. From the source:
// This metadata importer is exposed publically via CoCreateInstance(CLSID_CorMetaDataDispenser...).
// when the target is a .winmd file. It applies a small number of on-the-fly
// conversions to make the .winmd file look like a regular .NET assembly.
My take is that you can use CoCreateInstance(CLSID_CorMetaDataDispenser...)
to create an IMetaDataDispenser
, call OpenScope() with IID_IMetaDataImport
with a .winmd
file to get the metadata importer. It will then do conversions like IMap
to IDictionary
for you as you peruse the metadata. I speculate, but I'm pretty sure that's what Visual Studio does when generating type definitions from metadata or interface implementations.
You can also include the header with the macro iterator into your own C/C++ project and transform the data therein into whatever form is most useful for you, e.g. generate code from it.
来源:https://stackoverflow.com/questions/10679515/winrt-projected-types-documentation