I have the following WPF code:
public Window1()
{
InitializeComponent();
FlowDocument fd = new FlowDocument();
fd.LoadFromWordML("../../testdoc.docx");
FlowDocument (in System.Windows.Documents namespace) does not have a LoadFromWordML method. Rather this method is defined locally as follows:
public static void LoadFromWordML(this FlowDocument doc, string path)
Yet as you can see, LoadFromWordML is being called as if it is a method of FlowDocument class, and in fact the ‘this’ argument is being suppressed.
What C# language facility allows this? (And a comment against language design: this makes it very difficult to read legacy code.)
This is called extension methods
I have encountered these problems using them
Arcderiving fromCircle. Somebody decided thatTransformmethod should be an extension method. So most of the time it worked. Transforming aCircleyielded a transformedCircleand same withArc. But it lead to subtle errors when you transformed anArcwhen it was being used as anCirclereference.