I have an API that returns XElement‘s, and I want the document behind those XElement‘s to be immutable (read-only). I need it for:
- Not to give devs an ability to change it accidentally 🙂
- Improving performance – creating a copy of an
XDocumentmight be a performance “heavy” operation in some cases.
It doesn’t seem to possible to inherit & override the necessary behavior in XDocument/XElement/XContainer, because all virtual methods there are marked as internal:
internal virtual void XContainer.AddAttribute(XAttribute a)
{
}
So my question is – is there a way make it happen, or it is better to have a different API that will either return something like XPathNavigator‘s, or it is better to have own classes like IReadOnlyXElement, etc.?
You could create an
XElementwrapper that is similar toReadOnlyCollection<T>.