Portable Class Library Profile 78 missing attribut

2019-04-07 16:06发布

问题:

In my PCL core project (WP8, Android, iOS, Xamarin, MvvmCross) I use custom attributes. The Type.GetCustomAttributes() extension method lets me examine the attributes used.

Using PCL Profile104 this works well. But because I want to use async/await, I'll need to use PCL Profile78 (and .NET 4.5)

Problem: Seems the GetCustomAttributes() and the Attributes property are not available in Profile78. Why??

Note: I am looking into the workaround by creating a PCL Profile 104 class library and wrapping the GetCustomAttributes() and then referencing this library from my PCL Profile78 library. However it seems extensionmethods are not supported...

Example Code:

public Pcl78Class()
{
    Type t = this.GetType();
    var attributes = t.Attributes;
    var customAttributes = t.GetCustomAttributes(true);

    // another weird thing: Why is VS CodeCompletion telling me it knows CustomAttributeExtensions class and methods?
    //System.Reflection.CustomAttributeExtensions.GetCustomAttributes(t);
}

回答1:

Problem: Seems the GetCustomAttributes() and the Attributes property are not available in Profile78. Why??

Profile78 includes support for Windows Store 8 (as noted on my blog), and Windows Store has a more efficient implementation of Type-based reflection. Essentially, you just have to call Type.GetTypeInfo to get a TypeInfo, and from there it should be pretty straightforward.