How do you control the types of objects you can add to an XElement?
Is there a way to create a class that derives from XElement, but also control the types of objects that can be added to it?
For example, say I have this...
public class HtmlHead : XElement {}
But I can't override the Add() method because it's down at the XContainer level. And even if I was to create a class that derives from XContainer I still don't have access to override the Add() methods.
Why would I wa开发者_运维问答nt to do this, you ask?
I want to make sure that if my program compiles that it also parses the HTML correctly. Like, you can only add certain elements to an HTML <head> tag, so I want to make sure only supported tags can be added.
Surely, you won't be adding an <anchor> tag to the <head> tag—ya' feel me?
You can control the passed name via the constructor.
public class HtmlHead : XElement
{
public HtmlHead(object content) : base("head")
{
this.Add(content);
}
public HtmlHead(params object[] content) : base("head", content) { }
}
When an item is added, change notification is performed or you can create a custom method for adding which everyone should use.
加载中,请稍侯......
精彩评论