AX7 / D365 Extensions: Add a Method to a Class
Today I want to start a small series of blog posts. In this series I would like to show you how to extend AX 7 / D365 objects, without overlayering them, starting with an easy class extension.
Class extension 1.0
For this demo I created an extreme simple class, it only contains one method, which just puts out an infolog.
class BaseClass | |
{ | |
public void BaseClassMethod() | |
{ | |
info("Hello from the origin!"); | |
} | |
} |
To extend this class we have to add a new class to our project. I named it like the class to extend and added an “_Extension” to it. There are two important things to do. First we have to add the [ExtensionOf] attribute, second we have to make the extension class final. I also put in a new method, which also will info a slightly different message than the base class.
[ExtensionOf(classStr(BaseClass))] | |
final class BaseClass_Extension | |
{ | |
public void ExtensionMethod() | |
{ | |
info("Hello from the extension!"); | |
} | |
} |
Next I added a runnable class (in AX2012 a “Job”) to my project.
class RunMyClasses | |
{ | |
static void main (Args _args) | |
{ | |
BaseClass baseClass = new BaseClass(); | |
baseClass.BaseClassMethod(); | |
baseClass.ExtensionMethod(); | |
} | |
} |
In the screenshot below you can see how the IntelliSense in Visual Studio shows the “ExtensionMethod”, a very neat feature.
If you run the job, you should see the following result.
As you can see, it is very easy to add a new method to an existing class, without overlayering the original class. I admit this example is a very easy one, but I think you can build upon it. Let me know if you have any question.
In the next post we will take a look on tables.