Skip to content

Instantly share code, notes, and snippets.

@jeffwilcox
Created April 20, 2012 22:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeffwilcox/2432351 to your computer and use it in GitHub Desktop.
Save jeffwilcox/2432351 to your computer and use it in GitHub Desktop.
WinRT IsInstanceOfType, IsAssignableFrom
/// <summary>
/// Determines whether the specified object is an instance of the current Type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="o">The object to compare with the current type.</param>
/// <returns>true if the current Type is in the inheritance hierarchy of the
/// object represented by o, or if the current Type is an interface that o
/// supports. false if neither of these conditions is the case, or if o is
/// null, or if the current Type is an open generic type (that is,
/// ContainsGenericParameters returns true).</returns>
public static bool IsInstanceOfType(this Type type, object o)
{
return o != null && type.IsAssignableFrom(o.GetType());
}
internal static bool ImplementInterface(this Type type, Type ifaceType)
{
while (type != null)
{
Type[] interfaces = type.GetTypeInfo().ImplementedInterfaces.ToArray(); // .GetInterfaces();
if (interfaces != null)
{
for (int i = 0; i < interfaces.Length; i++)
{
if (interfaces[i] == ifaceType || (interfaces[i] != null && interfaces[i].ImplementInterface(ifaceType)))
{
return true;
}
}
}
type = type.GetTypeInfo().BaseType;
// type = type.BaseType;
}
return false;
}
public static bool IsAssignableFrom(this Type type, Type c)
{
if (c == null)
{
return false;
}
if (type == c)
{
return true;
}
//RuntimeType runtimeType = type.UnderlyingSystemType as RuntimeType;
//if (runtimeType != null)
//{
// return runtimeType.IsAssignableFrom(c);
//}
//if (c.IsSubclassOf(type))
if (c.GetTypeInfo().IsSubclassOf(c))
{
return true;
}
//if (type.IsInterface)
if (type.GetTypeInfo().IsInterface)
{
return c.ImplementInterface(type);
}
if (type.IsGenericParameter)
{
Type[] genericParameterConstraints = type.GetTypeInfo().GetGenericParameterConstraints();
for (int i = 0; i < genericParameterConstraints.Length; i++)
{
if (!genericParameterConstraints[i].IsAssignableFrom(c))
{
return false;
}
}
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment