Search code examples
c#windows-runtimecustom-attributessystem.reflectionportable-class-library

Custom Class Attributes in Metro Style App


I am trying to define and retrieve custom attributes on a class in a Metro Style App portable library.

Something like

[AttributeUsage(AttributeTargets.Class)]
public class FooAttribute : Attribute
{
}

[Foo]
public class Bar
{
}


class Program
{
    static void Main(string[] args)
    {
        var attrs = CustomAttributeExtensions.GetCustomAttribute<FooAttribute>(typeof(Bar));
    }
}

This works in ordinary 4.5, but in a portable library targetting metro style apps it tells me

Cannot convert type 'System.Type' to 'System.Reflection.MemberInfo'

Thanks


Solution

  • Or, too leverage extensions as they were meant:

    var attr = typeof(Bar).GetTypeInfo().GetCustomAttribute<FooAttribute>();