Access Protected members of standard class in Extension Class in (without overlayed) AX7 (Dynamics 365 for Operations)


Hi,

Goal here is to access Protected members purchTable of standard class PurchTableType  in Extension Class Custom_PurchTableType_Extension

Step 1 Create Extension class with new update method

[ExtensionOf(classStr(PurchTableType))]
final class Custom_PurchTableType_Extension
{
 public void CustomupdatePurchStatus(PurchTable purchTable1)
    {
purchTable.Status=1; //Open Order (for testing)
         purchTable.doupdate();
    }
}

Step 2  -- Trigger the update of PurchTable

Go to PurchTableType Class --> Methods --> Right Click on updateBackStatus --> Copy Post Event Handlers
add the code in to below class (PurchTableTypeEventHandlers)

Using System.Reflection; //Reflection to access Protected Members
class PurchTableTypeEventHandlers
{
    [PostHandlerFor(classStr(PurchTableType), methodStr(PurchTableType, updateBackStatus))]
    public static void PurchTableType_Post_updateBackStatus(XppPrePostArgs args)
    {
//Code to access purchTable Protected Member using Reflection
        PurchTableType purchTableType = args.getThis();

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

        var field = purchTableType.GetType().GetField("purchTable",bindFlags);

        PurchTable purchTable = field.GetValue(purchTableType);

        if(purchTable)
        {
   purchTableType.CustomupdatePurchStatus(purchTable);
        }
     
    }
}

Reference
Reference

using System.Reflection;
[ExtensionOf(classStr(VanillaClass))]
final class SomeFormVanillaClass_Extension
{
        VanillaClass      vanillaClass= Args.getThis();
        MyVariable        myVariable; // same name as variable in vanilla class

        var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic;
    
        var buffer = vanillaClass.GetType().GetField("myVariable",bindFlags);

        myVariable buffer .GetValue(vanillaClass);
}

Comments