I’m looking for the is operator, except that the type operand (right) is dynamic.
public static bool Is(this object value, Type type)
{
if (type == null) throw new ArgumentNullException(type, "type");
if (value == null) return false;
var valueType = value.GetType();
return valueType == type || valueType.IsSubclassOf(type)
|| valueType implements interface
}
Is there a simpler way of doing it?
I tried using IsAssignableFrom, but it doesn’t seem to be working:
var x = "asdf";
Console.WriteLine(x.GetType().IsAssignableFrom(typeof(object)));
Console.WriteLine(x is object);
It sounds like you are looking for
IsAssignableFrom: