5) Private, Protected and Public attribute access in Class Extension

Private, Protected and Public attribute access in Class Extension


lots of developer faced the issue on daily basis during development to access private, protected and public method.

As everyone know that public attribute is available to access with class instance.
but protected and private attribute are little difficult to access.

In this demo we show how to access all 3 access modifier using class extension.


Step-1 Create a class SLD_DemoClass.
Step-2 Declare 3 variable like below and one public method to set value in these variable.





Step-3 Now create Extension of this class with SLD_DemoClass_Extension.
Step-4 Create getter setter to access the variable.



for public and protected method its easy to access just to write simple getter setter method and can access these public methods using class instance.

Step-5 To Access private attribute in class extension, We need to use C# reflection library.

Step-6 Now write the below code to access private attribute on run-time.




following is the screenshot of the complete code of extension class.




Step-7 Now create a run-able class and action menu item to execute this code.




Step-8 perform build & Sync and login on D365FO for verification.



You can check in the screenshot all 3  protected, private and public attributes values in info message.

Comments

Popular posts from this blog

Customization on Sales invoice Report in D365 F&O

75) COC - Create a coc of the table modified method

46) D365 FO: SHAREPOINT FILE UPLOAD USING X++