As declared in the introduction of the SpiderInNet1, the very first post of the blog, though he mainly focuses on the AutoCAD .NET he sometimes crawls into the Microsoft .NET too to find something good and fast to digest there.
It is obvious why. The AutoCAD .NET is built upon the Microsoft .NET Framework, just like an extra brick somewhere on top of the .NET wall. Without the .NET Framework, the AutoCAD .NET just like a sleeve detached from a shirt or a roof without walls to support it.
Ok, now let us crawl with him to find something good and fast to digest in the .NET foundation.
Knowing it or not, people use Extension Methods now and then even if they do not create their own. For example, LINQ stuffs are almost all Extension Methods to various .NET existing types, and AutoCAD .NET provides similar stuffs too though not so obvious and neither well documented.
In short, an Extension Method is generally nothing more than a public and static method defined in a static class with its first argument modified with the ‘this’ keyword.
Here is an example.
namespace Extension1Namespace
{
public static class Extension1
{
public static string Concat1(this string first, string second)
{
first += second;
return first;
}
}
}
Moreover, in case it follows the pattern, it can be placed anywhere, i.e. the namespace does not matter. Let’s create a similar Extension Method in another namespace anyway.
namespace Extension2Namespace
{
public static class Extension2
{
public static string Concat2(this string first, string second)
{
first += second;
return first;
}
}
}
When calling the Extension method, we can call it against any instance of the type as specified with the ‘this’ keyword to make it look very like a native method of the type but please remember it is NOT.
In addition, we can still call the Extension method using the ordinary way, calling the full qualified method name and providing an type instance to the ‘this’ argument.
Let’s see some example code to demonstrate both for the two Extension Methods as defined in the two different namespaces above.
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using Extension1Namespace;
using Extension2Namespace;
namespace MyTestNamespace
{
public partial class FormX : Form
{
…
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(string.Concat("Hello! ", "How are you?"));
MessageBox.Show("Hello! ".Concat1("How are you?"));
MessageBox.Show(Extension1.Concat1("Hello! ", "How are you?"));
MessageBox.Show("Hello! ".Concat2("How are you?"));
MessageBox.Show(Extension2.Concat2("Hello! ", "How are you?"));
}
}
}
Each call yields the same result as the following.
By the way, the first call is to demonstrate the string.Concat() static method a bit. We can ignore it.
So, now things are clear, at least, much clearer than before, I believe. Extension Methods can be invoked in two ways, the real Extension Method way and the ordinary way. In case the real Extension Method way is used, we don’t really have to care about the namespace in which the Extension Method is defined in case the method signature is not changed and its namespace has been imported before the invocation. If the namespace is changed, changing the import namespace is enough. In case the ordinary invocation way is used for Extension Methods, I would wonder why.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Recent Comments