Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Is it possible to acce...
Finance forum

Is it possible to access private variables in Chain of command method without using System.Reflection?

(0) ShareShare
ReportReport
Posted on by 45

Hi Experts!

I'm customizing a class right now specifically the BusinessEventsWorkFlowWorkItem class wherein I need to access a private variable in the buildContract(). So basically I am accessing the private variable using a hack -> using System.Reflection and using the code below:

public void exposePrivateVariable()
{
     var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic;
     var buffer = this.GetType().GetField("nameOfThePrivateVariable", bindFlags);

     if (buffer)
     {
         nameOfThePrivateVariableTableBuffer = buffer.GetValue(this);
     }
}

Basically this is not accepted especially with the current AppChecker: https://github.com/microsoft/Dynamics365FO-AppChecker

Are there any other ways to get through this?

  • Henry Boco Profile Picture
    Henry Boco 45 on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    Hello, Mr. Drab.

    Thank you so much for taking time to help me with this. It surely worked like a charm!

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,466 Most Valuable Professional on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    I think I see a better way - save and relatively easy. ID of workflowWorkItemTable is passed to BusinessEventsWorkflowWorkItemDataContract, which is returned from buildContract(). Just get the object (cast it to BusinessEventsWorkflowWorkItemDataContract, because the declared type is BusinessEventsContract), call parmWorkflowWorkItemInstanceId() to the the ID, convert it to guid and call WorkflowWorkItemTable::find() to get the whole record.

    No need to take dangerous dependencies on private implementation details and giving up compile-time control.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,466 Most Valuable Professional on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    "I need this variable" is not a business requirement. It's how you've decided to solve the (unknown) requirement and unfortunately this design is wrong (as discussed above), therefore you should take a step back and think again about the how the business problem can be solved.

    Maybe  you don't have to redesign the whole process, it might be sufficient to look at the implementation from a slightly different point of view (with a catch that I'll mention later). Where is workflowWorkItemTable coming from? From the constructor. It's private too, so it won't help, but which method calls the constructor? The construct() method, which is public, therefore you can use CoC on it and access _workflowWorkItemTable parameter to get what you want! Now there is a question what you'll do with the value. I would add another instance variable (and a parm method) in the extension class of BusinessEventsWorkFlowWorkItem and set the value in the extension of construct(). Then you can call the custom parm method from everywhere.

    What's worrying is that the whole class is internal, therefore it shouldn't be accessible at all. That it works may be a bug that will be fixed later, breaking all your code depending on it. Therefore maybe you really should rethink the whole idea.

  • Henry Boco Profile Picture
    Henry Boco 45 on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    Hello, Mr. Blue Wang.

    Thank you so much for replying to my post. This is the current implementation that I used. This works definitely, but not a best practice. So I am currently looking for other workarounds in order to get this by.

  • Henry Boco Profile Picture
    Henry Boco 45 on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    Hello, Mr. Sukrut.

    Thank you for dropping by my post. Really appreciate it.

  • Henry Boco Profile Picture
    Henry Boco 45 on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    Hello, Mr. Drab.

    Really appreciate your response. It means a lot.

    Basically I am trying to get below items from the private variable "workflowWorkItemTable":

    1. Getting the common table where the workflowWorkItemTable private variable came from. So I used the code below to get it:

    commonTable = workflowWorkItemTableTableBuffer.getRecord();

    2. I would also need to get the workflowWorkItemTable private variable itself and it's records

    So to conclude, without the private variable "workflowWorkItemTable", I won't be able to proceed with the desired customization.

  • Blue Wang Profile Picture
    Blue Wang on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    Hi Kali,

    First, Martin and Sukrut have already answered your question.


    Private variables are not accessible in COC.


    You can check if these are useful to you.

    https://sites.google.com/site/dynamicsaxblog/how-to-list-ax7/howtoaccessprotectedvariablesfromanextensionclass

    https://community.dynamics.com/365/financeandoperations/b/ievgensaxblog/posts/ax-7-accessing-private-protected-class-methods-and-members-from-extension-code

    https://community.dynamics.com/365/financeandoperations/f/dynamics-365-for-finance-and-operations-forum/314156/accessing-the-variable

  • Suggested answer
    Sukrut Parab Profile Picture
    Sukrut Parab 71,671 Moderator on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    Well the documentation clearly says , you cant and using reflection can break anytime in future when Microsoft releases updates . So better you can think of some alternative solution.

  • Martin Dráb Profile Picture
    Martin Dráb 230,466 Most Valuable Professional on at
    RE: Is it possible to access private variables in Chain of command method without using System.Reflection?

    If you say you must access a private variable, you've already lost the battle. The question is: can't you solve the business problem without accessing private variables?

    What is it that you're unable to achieve?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans