10

Próbuję zdefiniować i pobrać niestandardowe atrybuty klasy w bibliotece przenośnej aplikacji Metro Style App.Niestandardowe atrybuty klas w aplikacji Metro Style

Coś

[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)); 
    } 
} 

Działa to w zwykłym 4,5, ale w przenośnej biblioteki targetting aplikacje w stylu Metro mówi mi

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

Dzięki

+0

Widocznie trzeba zrobić var attrs = CustomAttributeExtensions.GetCustomAttribute (typeof (Bar) .GetTypeInfo()); –

+1

http://msdn.microsoft.com/en-us/library/windows/apps/hh535795%28v=vs.110%29.aspx –

+1

@ user1364325, jeśli kod w powyższym komentarzu rozwiązuje problem, opublikuj go jako odpowiedź i zaznacz to jako odpowiedź. – Adam

Odpowiedz

4

Lub też rozszerzeniami dźwigni, gdyż były one oznaczało:

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

Według OP:

You n eed zrobić var ​​attrs = CustomAttributeExtensions.GetCustomAttribute (typeof (Bar) .GetTypeIn fo());

To wydaje się zgodzić z the documentation

Powiązane problemy