34

Is there any alternative for IsSubclassOf or IsAssignableFrom in C# Metro-style?

I'm trying to make this code run on Metro but can't find alternative.

if ((ui.GetType() == type) || (ui.GetType().IsSubclassOf(type)))
{
    return true;
}
Kevin Vermeer
  • 2,736
  • 2
  • 27
  • 38
Michael Sync
  • 4,834
  • 10
  • 40
  • 58

2 Answers2

55

Many of the reflection methods can be found in the System.Reflection.TypeInfo class.

You can get an instance of TypeInfo for your Type using the GetTypeInfo extension method, provided by System.Reflection.IntrospectionExtensions:

using System.Reflection;

// ...

ui.GetType().GetTypeInfo().IsSubclassOf(type)
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Many of reflection methods are not available in metro-style windows 8 .net framework. This is the reason that I was looking for alternative. but I think there is no alternative so seems like have to use "is" or "as" with explicit type. – Michael Sync Jan 09 '12 at 09:51
  • @MichaelSync: I don't understand why you say there is no alternative. Does `TypeInfo` not work for you? If not, why not? – James McNellis Jan 09 '12 at 16:58
  • Please take a look at metro .net reference. http://msdn.microsoft.com/en-us/library/windows/apps/hh441595(v=vs.110).aspx .. a lot of types are missing there.. – Michael Sync Jan 25 '12 at 19:24
  • 11
    What, exactly, are you looking for? If you don't state what you need to do, I can't help. Your original question was whether there was an alternative for `IsSubclassOf` and `IsAssignableFrom`... the answer to that is yes: they exist, in `TypeInfo`. – James McNellis Jan 25 '12 at 19:27
17

You can use this:

using System.Reflection;

// ...

ui.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo());

This works in Metro.

Mualig
  • 1,444
  • 1
  • 19
  • 42
Rhett
  • 171
  • 1
  • 2