diff --git a/src/XPath2/XPath2Expression.cs b/src/XPath2/XPath2Expression.cs index f0e9b9f..c9c1c9e 100644 --- a/src/XPath2/XPath2Expression.cs +++ b/src/XPath2/XPath2Expression.cs @@ -53,21 +53,18 @@ private object PrepareValue(object value) { if (value == null) return Undefined.Value; - XmlNode xmlNode = value as XmlNode; - if (xmlNode != null) + + if (value is XmlNode xmlNode) return xmlNode.CreateNavigator(); - XNode xnode = value as XNode; - if (xnode != null) + else if (value is XNode xnode) return xnode.CreateNavigator(); - XPath2NodeIterator iter = value as XPath2NodeIterator; - if (iter != null) + else if (value is XPath2NodeIterator) return iter; - IEnumerable en = value as IEnumerable; - if (en != null) + else if (value is IEnumerable en) return new NodeIterator(CreateIterator(en)); - IEnumerable eno = value as IEnumerable; - if (eno != null) + else if (value is IEnumerable eno) return new NodeIterator(CreateIterator(eno)); + return value; } @@ -112,9 +109,11 @@ public static IEnumerable Select(string xpath, IXmlNamespaceResolver resol { XPathNavigator curr = (XPathNavigator)item; XObject o = (XObject)curr.UnderlyingObject; - if (!(o is T)) + + if (o is T t) + yield return t; + else throw new InvalidOperationException(String.Format("Unexpected evaluation {0}", o.GetType())); - yield return (T)o; } else throw new InvalidOperationException(String.Format("Unexpected evaluation {0}", item.TypedValue.GetType())); @@ -191,16 +190,17 @@ private object[] BindExpr(IDictionary vars) public object Evaluate(IContextProvider provider, IDictionary vars) { object res = ExpressionTree.Execute(provider, BindExpr(vars)); - if (res is XPathItem) + if (res is XPathItem item) { - XPathItem item = (XPathItem)res; if (!item.IsNode) res = item.TypedValue; } + resultType = CoreFuncs.GetXPath2ResultType(res); - ValueProxy proxy = res as ValueProxy; - if (proxy != null) + + if (res is ValueProxy proxy) return proxy.Value; + return res; } @@ -211,7 +211,8 @@ public object EvaluateWithProperties(IContextProvider provider, object props) { Type type = props.GetType(); if (type.IsArray) - throw new ArgumentException("props"); + throw new ArgumentException(nameof(props)); + PropertyInfo[] propsInfo = type.GetProperties(); vars = new Dictionary(propsInfo.Length); for (int k = 0; k < propsInfo.Length; k++) @@ -232,4 +233,5 @@ public XPath2ResultType GetResultType(IDictionary vars return resultType.Value; } } -} \ No newline at end of file + +}