none
Type.IsAssignableFrom in Portable Class Library RRS feed

  • Question

  • Hi everyone,

    The documentation states that Type.IsAssignableFrom is portable.  Furthermore, this spreadsheet, retrieved from this blog post, states that it's supported on every platform.

    Yet, I'm getting the following error in a portable library project targeting .NET 4.5 and WinStore only:

    Error 36 'System.Type' does not contain a definition for 'IsAssignableFrom' and no extension method 'IsAssignableFrom' accepting a first argument of type 'System.Type' could be found [snip]

    But what's really strange is that when I include Silverlight 5 the error goes away; i.e., there's no error when my library targets .NET 4.5, WinStore and SL5.  Yet there's an error when my library only targets .NET 4.5 and WinStore.

    I cannot support SL5 because my portable library depends upon another portable library that doesn't support SL5.

    This is quickly becoming a common problem for me and apparently others, regarding reflection APIs in general.

    Am I missing something or is this some kind of bug?  Or is it just a documentation error?

    I'm running VS 2012 Pro.

    Thanks,
    Dave


    http://davesexton.com/blog

    Saturday, December 1, 2012 9:25 PM

Answers

  • Hi,

    I found a workaround, though it still doesn't explain why including SL5 avoids the error.  Actually, I think I remember reading a blog post a while ago about strange behavior like this, related to development time constraints on the PCL or other general "surface area" issues that were basically just trade-offs of some kind.  Anyway, it would be great if this could be resolved in a future VS update.

    Solution:

    Add the following using statement:

    using System.Reflection;

    That puts the GetTypeInfo extension method in scope.  Now you can write code like this:

    if (typeA.GetTypeInfo().IsAssignableFrom(typeB.GetTypeInfo())
    {
      // ...
    }

    - Dave


    http://davesexton.com/blog

    • Marked as answer by Dave Sexton Saturday, December 1, 2012 10:24 PM
    Saturday, December 1, 2012 10:23 PM

All replies

  • Hi,

    I found a workaround, though it still doesn't explain why including SL5 avoids the error.  Actually, I think I remember reading a blog post a while ago about strange behavior like this, related to development time constraints on the PCL or other general "surface area" issues that were basically just trade-offs of some kind.  Anyway, it would be great if this could be resolved in a future VS update.

    Solution:

    Add the following using statement:

    using System.Reflection;

    That puts the GetTypeInfo extension method in scope.  Now you can write code like this:

    if (typeA.GetTypeInfo().IsAssignableFrom(typeB.GetTypeInfo())
    {
      // ...
    }

    - Dave


    http://davesexton.com/blog

    • Marked as answer by Dave Sexton Saturday, December 1, 2012 10:24 PM
    Saturday, December 1, 2012 10:23 PM
  • It appears that the Type class is being replaced by the TypeInfo class in 4.5 The method you require is defined in that class, taking one parameter of type Type:

    http://msdn.microsoft.com/en-us/library/system.reflection.typeinfo.aspx


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer


    Saturday, December 1, 2012 10:23 PM
  • Hi Pieter,

    Thanks, I discovered it on my own.

    Note that it doesn't actually answer the question of why including SL5 makes the error go away.  :)

    - Dave


    http://davesexton.com/blog

    Saturday, December 1, 2012 10:34 PM