No items found.

Invoking private methods outside declaring class in AX 2012

In short, public allows for  unrestricted use by any classes, protected – only by derived and declaring, private narrows access to declaring class. So, by theory, we shouldn’t be able to access private method outside declaring class. In X++ this is true and false at the same time. How is it possible? Surprisingly, it is very simple. We only need to cast an instance of our class to the object type – base class from which all other classes are derived. Please apply and run following (pseudo) code and you will see.

‍

class TestClass1

{

public void publicMethod()

{

   info("Hey, I'm public!");

}

protected void protectedMethod()

{

   info("Hey, I'm protected!");

}

private void privateMethod()

{

   info("Hey, I'm private!");

}

}

class TestClass2

{

public static void main(Args args)

{

   TestClass1 class1;

   Object objectClass1;

   class1 = new TestClass1();

   objectClass1 = class1;

   objectClass1.publicMethod();

   objectClass1.protectedMethod();

   objectClass1.privateMethod();

}

}

I personally see this functionality as a bug, but quite useful one. It happened to me already few times, that I had to invoke some private methods. Normally I would overwrite interested object and change its access modifier. But this approach is definitely not elegant. Firstly, having copy in upper layer can cause some problems when upgrading. Secondly, we globally and explicitly change developer’s will.

Summarizing, both solutions should be deeply considered before use, but one of them seems to be much easier to implement.

There is more. Similarly, we can also invoke private table methods. Of course instead of object type a common must be used.

‍

Zobacz inne

Hurtownia Danych 2.0.

Hurtownia danych w Microsoft Fabric jako dźwignia wzrostu.

Czytaj artykuł
Text Link
Dynamics 365 F&O

200 tysięcy, 400 tysięcy, milion euro. Ile naprawdę kosztuje przechowywanie historii w D365 F&O.

Czytaj artykuł
Text Link
Power BI

Szkolenie Power BI: zakres, poziomy i certyfikacja PL-300

Czytaj artykuł
Text Link
Wróć do wszystkich

Szukasz partnera Microsoft? Porozmawiajmy o Twoim projekcie

Pracuj z zespołem, który zrealizował wdrożenia i projekty dla dziesiątek firm. Umów rozmowę wstępną i sprawdź:

Jak Dynamics 365 dopasować do procesów Twojej firmy, a nie odwrotnie

Które obszary warto zautomatyzować, żeby odzyskać czas i obniżyć koszty

Jak wygląda wdrożenie z Anegis krok po kroku: od analizy po wsparcie po starcie

Wybierz termin spotkania
Dziękujemy za wiadomość!
Oops! Something went wrong while submitting the form.
Microsoft AI Builder
Microsoft Copilot Studio
No items found.
No items found.
No items found.