Search code examples
c#.netportable-class-librarywin-universal-app

Check if a type defined by a Type variable implements an interface in .NET Portable


I have portable component in my universal app, and thus, the component uses .NET Portable v.4.6. In that component, I'm trying to check if the type defined by a Type variable myType implements a certain interface IMyinterface.

If I were using the standard .NET framework, I could check if myType.GetInterface("MyClass.IMyinterface") != null

or if

(typeof(IMyinterface).IsAssignableFrom(myType)) == true

(see: http://www.hanselman.com/blog/DoesATypeImplementAnInterface.aspx)

However, these methods are not available in .NET Portable v.4.6. Any ideas how I could do this check in this case?


Solution

  • I'd guess you need to add the following using:

    using System.Reflection;
    

    and then do the check like this:

    (typeof(IMyinterface).GetTypeInfo().IsAssignableFrom(myType.GetTypeInfo())) == true