Dealing with types in WinRT

.NET, Build, Technical stuff, Windows 8, Work
See comments

Another quick tip: Most of the interaction with the System.Type class in Windows 8 is now done through the TypeInfo class. An instance of TypeInfo for a give type is retrieved using an extension method called GetTypeInfo(). Note that since this is an extension method, it will not be visible in Intellisense unless you add a reference to System.Reflection in the “using” section:

// Needed for the extension method
using System.Reflection;

(...)

var info = myType.GetTypeInfo();

// Was: string name = myType.Name
string name = info.Name;

// Was: Type[] interfaces = myType.GetInterfaces();
IEnumerable<Type> interfaces = info.ImplementedInterfaces;

// Was: bool check = myType.IsSubclassOf(anotherType);
bool check = anotherType.GetTypeInfo().IsAssignableFrom(info);

(etc...)

Note: IsSubclassOf and IsAssignableFrom do not have the exact same meaning, however in many cases it should be able to use IsAssignableFrom. Make sure to check the documentation to verify that this is indeed what you want!

Hopefully this is useful to WinRT coders out there!

Laurent

Previous entry | Next blog entry

Comments for Dealing with types in WinRT