Simple request.
Could the Node object have a property named “AsCompositeNode” that is null or just the node itself already cast as a CompositeNode.
This would allow avoiding having to recast the node.
Instead of :
var aCNode = (CompositeNode)aNode;
to be replaced with
var ACNode = aNode.AsCompositeNode;
Allowing:
if (aNode.IsComposite && aNode.AsCompositeNode.HasChildNodes) {…}
Thanks,
Andy Bay
I’m not sure how to elaborate more than above.
I just want to avoid recasting using
CompositeNode objCompositeNode = (CompositeNode)RandomNodeObject;
and instead have access to it through a property such as
CompositeNode objCompositeNode = RandomNodeObject.AsCompositeNode;
Thus avoiding ugly, distracting, and inconvenient parenthesis.
If the “RandomNodeObject” happens to not be a Composite Node, then the property is null.
Edit:
Feature Request:
Add to the Node Object:
public CompositeNode AsCompositeNode
{ get {
if (IsComposite) return (CompositeNode)this;
else return null;
}
};
Thanks,
Andy Bay
Examples:
if (UpdateElement.IsComposite && UpdateElement.AsCompositeNode.GetChildNodes(NodeType.Table, true).Count > 0)
{…}
—
if (hayStack.IsComposite)
{
var kids = hayStack.AsCompositeNode.ChildNodes;
…
}
—
—
Previously, these two calls ended up needed ugly nested parenthesis:
IE
var kids = ((CompositeNode)hayStack).ChildNodes;
And, just for fun, an Extension Method that does it (because you can’t create extension properties.):
///
/// Add a helper functions to the ASPOSE Node object.
///
public static class NodeExtensions
{
///
/// Easier Access to the Composite Node of a Node.
///
/// Node that may be a Composite Node
/// Composite Node or Null
public static CompositeNode AsCompositeNode(this Node thisNode)
{
if (!thisNode.IsComposite)
return null;
return (CompositeNode)thisNode;
}
}
There are a number of methods that return “Node” in some fashion such as GetChild, GetChildNodes, SelectNodes, SelectSingleNode, GetAncestor, NextPreOrder, PreviousPreOrder, NextSibling, and PreviousSibling.
The majority of nodes you are going to get are Composite Nodes. I am frequently having to recast Nodes into Composite Nodes. I just want a handy property that does this for me rather than ending up with clunky code that has nested parenthesis to recast and access CompositeNode properties in one-off statements.
((CompositeNode)node).GetChildNodes(…) is ugly.
node.AsCompositeNode.GetChildNodes(…) is clean looking.
Thank You,
Andy Bay