locked
WinRT attributes are not available via TypeInfo.GetCustomAttributes?

    Question

  • Perhaps I am doing something wrong but it seems that WinRT attributes decorated on a class are not included when querying the custom attributes. I already read this post about using TypeInfo which I already was using. So if for example I define my own attribute derived from System.Attribute or use one of the 'system' ones (e.g. ClsCompliant) and put it on my MainPage, when use typeof(MainPage).GetTypeInfo().GetCustomAttributes().ToList() I get those attributes. However if I put a WinRT attribute (e.g. Windows.Foundation.Metadata.ContentPropertyAttribute or Windows.UI.Xaml.Markup.DefaultAttribute) on that same class - those WinRT attributes will not be included. Is there a different mechanism for getting those attributes or is this just a bug in the preview?

    Friday, October 14, 2011 7:55 PM

Answers

  • Sorry, I should have double checked the BUILD bits. Yes, you are correct that this API is missing. Right now, we are considering adding this API.

    Out of curiosity: what is your scenario for reading these attributes? Is this a runtime/design time scenario or are you writing some sort of internal tool that runs as part of your build process?


    Immo Landwerth | .NET Framework Team (BCL) | http://blogs.msdn.com/b/bclteam/
    Thursday, October 20, 2011 6:24 PM

All replies

  • The attributes exist purely in metadata. In other words, they are, but they are not instantiatable. There is no actual type to create an instance of, in WinRT attributes exist as pure metadata only. Therefore, the way you get them from managed code is via CustomAttributeData.


    Immo Landwerth | .NET Framework Team (BCL) | http://blogs.msdn.com/b/bclteam/
    Monday, October 17, 2011 10:31 PM
  • Perhaps I'm missing something but CustomAttributeData is not exposed in the WinRT profile.
    Tuesday, October 18, 2011 1:53 PM
  • Sorry, I should have double checked the BUILD bits. Yes, you are correct that this API is missing. Right now, we are considering adding this API.

    Out of curiosity: what is your scenario for reading these attributes? Is this a runtime/design time scenario or are you writing some sort of internal tool that runs as part of your build process?


    Immo Landwerth | .NET Framework Team (BCL) | http://blogs.msdn.com/b/bclteam/
    Thursday, October 20, 2011 6:24 PM
  • In this specific case I was doing it as part of a workaround to obtain the ContentPropertyAttribute at runtime - it used to be available in WPF/SL code since that was a .NET attribute - so I didn't understand why it wasn't being returned. There aren't a huge # of WinRT attributes from what I can see so it may not be the end of the world but it is definitely confusing that they look and smell like attributes and therefore should be available at runtime but there's no way to get to them.
    Thursday, October 20, 2011 6:38 PM
  • This appears to still be missing as of Windows 8.1, unfortunately.  Can someone from MSFT please comment on if this has been fixed and added to the Windows 10?  It is the surprising (and frustrating) lack of very basic fundamental features such as this that really give WinRT a very unpolished feeling to it (and part of the reason why adoption has been so slow).  It doesn't help that issues such as these have been around since 2011 and still haven't been addressed.

    :(

    • Edited by Mike-EEE Saturday, April 4, 2015 11:44 AM
    Friday, April 3, 2015 10:48 PM