web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
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?

I have the same question (0)
  • Henry Boco Profile Picture
    45 on at

    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
    238,322 Most Valuable Professional on at

    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
    238,322 Most Valuable Professional on at

    "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
    45 on at

    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
    45 on at

    Hello, Mr. Sukrut.

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

  • Henry Boco Profile Picture
    45 on at

    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
    on at
  • Suggested answer
    Sukrut Parab Profile Picture
    71,710 Moderator on at

    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
    238,322 Most Valuable Professional on at

    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

News and Announcements

Season of Giving Solutions is Here!

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 > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans