Reputation: 1504
When I run Gendarme 2.11 on my C#.NET project the following code triggers the EnsureLocalDisposalRule:
XmlDocument myXmlDoc = this.GetXmlDoc();
foreach (XmlNode myNode in myXmlDoc.GetElementsByTagName("TAGNAME"))
{
... does something with myNode ...
}
with the message:
Local of type 'XmlNodeList' is not disposed of (at least not locally).
After reading the rule description I attempted to rewrite it as follows:
XmlDocument myXmlDoc = this.GetXmlDoc();
using (XmlNodeList myNodeList = myXmlDoc.GetElementsByTagName("TAGNAME"))
{
foreach (XmlNode myNode in myNodeList )
{
... does something with myNode ...
}
}
but this gives the error:
'System.Xml.XmlNodeList': type used in a using statement must be implicitly convertible to 'System.IDisposable'
What is causing this? Is this a bug in Gendarme? Or have I misunderstood the rule? How could my code be improved?
Upvotes: 1
Views: 81