AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX: Программирование
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 07.11.2018, 17:11   #1  
wojzeh is offline
wojzeh
Участник
Аватар для wojzeh
Соотечественники
 
681 / 517 (19) +++++++
Регистрация: 27.04.2006
Адрес: Montreal
Wrapping a base method in an extension of a derived class
The following example shows how to wrap a base method in an extension of a derived class. For this example, the following class hierarchy is used.


X++:
class A
{
    public void salute(str message)
    {   
        info(message);
    }
}

class B extends A {}
class C extends A {}
Therefore, there is one base class, A. Two classes, B and C, are derived from A. We will augment or create an extension class of one of the derived classes (in this case, B), as shown here.


X++:
[ExtensionOf(classStr(B))]
final class B_Extension
{
    public void salute(str message)
    {
        next salute(message);
        info("B extension");
    }
}
Although the B_Extension class is an extension of B, and B doesn't have a method definition for the salute method, you can wrap the salute method that is defined in the base class, A. Therefore, only instances of the B class will include the wrapping of the salute method. Instances of the A and C classes will never call the wrapper method that is defined in the extension of the B class.

This behavior becomes clearer if we implement a method that uses these three classes.

X++:
class ProgramTest 
{
    public static void main(Args args)
    {
        var a = new A();
        var b = new B();
        var c = new C();

        a.salute("Hi");
        b.salute("Hi");
        c.salute("Hi");
    }
}
For calls to a.salute(“Hi”) and c.salute(“Hi”), the Infolog shows only the message “Hi.” However, when b.salute(“Hi”) is called, the Infolog shows “Hi” followed by “B extension.”

By using this mechanism, you can wrap the original method only for specific derived classes.

https://docs.microsoft.com/en-us/dyn...d-wrapping-coc
__________________
Felix nihil admirari

Последний раз редактировалось wojzeh; 07.11.2018 в 17:13.
Старый 07.11.2018, 18:30   #2  
malex is offline
malex
Участник
 
164 / 19 (1) ++
Регистрация: 10.08.2004
Адрес: Тверь, Москва
?
Цитата:
Сообщение от wojzeh Посмотреть сообщение
Wrapping a base method in an extension of a derived class
The following example shows how to wrap a base method in an extension of a derived class. For this example, the following class hierarchy is used:
Здравствуйте, wojzeh. Спасибо за ответ. У меня немного другая ситуация: у меня нет COC, но есть extension для базового класса и extension для наследника. Если переменная объявлена как базовый класс, но в realTime инициализируется как наследник, то extension все равно вызывается для базового класса.
(Thank you for your answer. I have the different case: I havn't COC. I have extensions for parent class and for a child. Both extensions has the same method. A class variable declared as a parent class, but initialized as a child. The system invokes extension for the parent class)
X++:
class A
{
}
class B extends A
{
}
[ExtensionOf(classStr(A))]
final class A_Extension
{
    public void salute()
    {
        info("A extension");
    }

}
[ExtensionOf(classStr(B))]
final class B_Extension
{
    public void salute()
    {
        info("B extension");
    }

}
X++:
class Test01
{        
    public static void main(Args _args)
    {    
        A a;

        a = new B(); 

        a.salute();
    }

}
В результате получим "A extension".
За это сообщение автора поблагодарили: Logger (5), Pokersky09 (2).
Теги
chain of command, d365fo

 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
erconsult: Copy-paste with keyboard script 2: from Excel to D365FO Blog bot DAX Blogs 0 03.08.2018 11:12
daxmusings: The Overall Concept of Extensions in AX 7 Blog bot DAX Blogs 0 14.09.2017 13:11
sertandev: AX7 Extensibility Overview – Part 2 : Code extensions Blog bot DAX Blogs 0 28.08.2017 19:11
daxmusings: The Overall Concept of Extensions in AX 7 Blog bot DAX Blogs 0 04.10.2016 12:11
goshoom: Class extensions Blog bot DAX Blogs 0 18.07.2016 17:11
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра
Комбинированный вид Комбинированный вид

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 20:16.