Thursday 21 December 2017

How to access private members through reflection in AX7 / D365

In AX7 / D365 by default all class members have protected access modifier. We can use these members in our custom classes through extentions but sometime it will become a real problem when we try to extend OOTB classes. 

So without using extensions of the class we have apparently two options:
1- Overlayering (Will not be available after Update 14 as per Microsoft)
2- Reflection 

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 some disadvantages:
1- It breaches entire basis of Object oriented programming.
2- Slow performance.
3- 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. In below mention images shows how we can use private 
variable by reflection technique.

OOTB Class:

Using reflection namespace:

Get private member:

1 comment:

  1. Hi Tabsheer, I am going to update a private member by reflection, do you know anyway to do that? Thanks & Regards.


How to Get Customer Free Text Invoice "Totals" form field value with X++ in D365FO

Customer free text invoices are an essential aspect of financial management in D365FO. In this blog, I have compiled all the code needed to ...