Is there a quick way to find out if an object variable’s contents supports IEnumerable? Specifically I’m using XPathEvaluate() from System.Xml.XPath, which can return “An object that can contain a bool, a double, a string, or an IEnumerable.“
So after executing:
XDocument content = XDocument.Load("foo.xml");
object action = content.XPathEvaluate("/bar/baz/@quux");
// Do I now call action.ToString(), or foreach(var foo in action)?
I could poke around with action.GetType().GetInterface(), but I thought I’d ask if there’s a quicker/easier way.
You are looking for the
isoperator:or even better, the
asoperator.Note that
stringalso implementsIEnumerable, so you might like to extend that check toif(enumerable != null && !(action is string))