AX 7. Accessing private\protected class methods and members from extension code.

All class member variables are protected by default in AX 7, so it is impossible to access them from extensions. It becomes a real problem when you try to extend classes like SalesLineType.

For example, we want to add custom logic on sales line insert event. Call to super() is commented out so we cannot create pre or post event handlers. We may try to create event handlers for SalesLineType.insert() mehtod, but we won’t get access to salesLine buffer because this variable is protected.

There are two options: use overlaying or use reflection.

Overlaying is preferable approach, but today we will talk about reflection to explain this option.

Reflection is usually used for unit testing in case you need to cover protected or private code and it is hard to call this code using public API.

It has bunch of disadvantages:

  • It breaches entire basis of OO programming.
  • Slow performance.
  • Possible issues with future updates. Private methods could be changed at any time.

However, once you may get into situation where it could be the only option so it’s better to know about this possibility.

Let’s try to change some fields on sales line insert using reflection.

Create new event handler class for SalesLineType and subscribe to post insert:

using System.Reflection;

/// Handles events raised by <c>SalesLineTypeEventHandler</c> class.
/// </summary>

public class SalesLineTypeEventHandler
    [PostHandlerFor(classStr(SalesLineType), methodStr(SalesLineType, insert))]
    public static void SalesLineType_Post_insert(XppPrePostArgs _args)
        SalesLineType salesLineType = _args.getThis();

        var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic;

        var field = salesLineType.GetType().GetField("salesLine", bindFlags);

        SalesLine salesLine = field.GetValue(salesLineType);

        if (salesLine)
            salesLine.MyNewField = 42;

Also we can call private or protected method:

var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic;

var methodInfo = salesLineType.GetType().GetMethod(methodStr(SalesLineType, checkQuantityUpdate), bindFlags);

if (methodInfo)
    methodInfo.Invoke(salesLineType,  new System.Object[0]());

You can read more about reflection on msdn

Thanks to Simon Buxton for rising this on yammer.

7 thoughts on “AX 7. Accessing private\protected class methods and members from extension code.

  1. xiety666 December 20, 2016 / 7:52 am

    Why not use more familiar DictClass.callObject to call private method? It is far more concisely when passing several parameters to the method.

    • ievgensaxblog December 24, 2016 / 7:17 am

      DictClass is an old school way, but as far as I know, it has no API to call private\protected class variables.

  2. Alok Shrestha May 9, 2017 / 6:39 pm

    Hi Ievgen,
    Have we figure out the better way to do this without using reflection and overlayering? Basically, what I am trying to do is initialize two fields in purchline while creating purchase order from purchase requisition.

    Thank you

  3. Sanjay August 12, 2017 / 6:19 pm

    How can we access protected variables declared in Vanilla class from an Extension class.
    Is it possible in D365?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s