Источник:
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/