Показать сообщение отдельно
Старый 16.11.2017, 10:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,487 / 846 (79) +++++++
Регистрация: 28.10.2006
ievgensaxblog: D365FOE. FormHasMethod extension for form extension methods.
Источник: https://ievgensaxblog.wordpress.com/...nsion-methods/
==============

Recently I have seen multiple people asking how to check if form has method added by extension at run-time.  For form methods we can use Global::formHasMethod but it does not work with form extensions. I advised people to use new metadata API to do this and finally community user Axaptus wrote the code!

I tweaked it a bit to ignore method’s name case as AX does and to exclude private methods. Also I used it to extend standard formHasMethod method

X++:
/// <summary>
/// The class <c>Global_Extension</c> contains extension methods for the <c>Global</c> class.
/// </summary>

[ExtensionOf(classStr(Global))]
public static final class Global_Extension
{
    static boolean formHasMethod(FormRun fr, IdentifierName methodName)
    {
        boolean ret = next formHasMethod(fr, methodName);

        if (!ret)
        {
            ret = Global::formExtensionHasMethod_IM(fr, methodName);
        }

        return ret;
    }

    private static boolean formExtensionHasMethod_IM(FormRun _formRun, IdentifierName _methodName)
    {
        if (!_formRun || !_methodName)
        {
            return false;
        }

        boolean ret = false;

        try
        {
            System.Object[] extensions = Microsoft.Dynamics.Ax.Xpp.ExtensionClassSupport::GetExtensionsOnType(_formRun.GetType(), true);

            if (extensions)
            {
                System.Type    formRunExtensionType;
                System.Reflection.MethodInfo    methodInfo;
var bindingFlags = BindingFlags::Public | BindingFlags::Instance | BindingFlags::Static | BindingFlags::IgnoreCase;
for (int i = 0; i < extensions.Length; i++)
{
                    formRunExtensionType = extensions.GetValue(i);

                    methodInfo = formRunExtensionType.GetMethod(_methodName, bindingFlags);

                    if (methodInfo)
                    {
                        ret = true;
                        break;
                    }
                }
            }
        }
        catch (Exception::CLRError)
        {
            error(CLRInterop::getLastException().ToString());
        }

        return ret;
    }
}
Extending standard method has its pros and cons. From one side it will slow down execution of standard code that calls it when method does not exist, but it’s a rare case. From another side it allows you to reuse standard code without changing it and it could be handy in various places where AX looks for a method on a form.

Source code is available on GitHub




Источник: https://ievgensaxblog.wordpress.com/...nsion-methods/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.

Последний раз редактировалось mazzy; 16.11.2017 в 10:19.
За это сообщение автора поблагодарили: Jorj (1).