Showing posts with label Reflection. Show all posts
Showing posts with label Reflection. Show all posts

FormHasMethod extension in D365FO

 FormHasMethod extension in D365FO


Every developer who has started working on D365FO faces this issue that when we create a method in the form of Extension

For verification of method exists or not in the run time, we can use Global::formHasMethod but it does not work with form extensions

So I advise everyone to use the below code is working... 


Source Link


using  System.Object;
using  Microsoft.Dynamics.Ax.Xpp;
using  System.Reflection;
    [ExtensionOf(classStr(Global))]
 final class Global_Extension
{
    static boolean formHasMethod(FormRun _fromRun, IdentifierName _methodName)
    {
        boolean flag= next formHasMethod(_fromRun_methodName);

        if (flag==true)
        {
            flag= Global::VerifyformExtensionHasMethod(_fromRun_methodName);
        }

        return ret;
    }

    private static boolean VerifyformExtensionHasMethod(FormRun _formRun, IdentifierName _methodName)
    {
        

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

            if (extensions)
            {
               System.Type    formExtensionType;
               MethodInfo    mInfo;
             
                var  bindingFlags = BindingFlags::Public | BindingFlags::Static | BindingFlags::IgnoreCase;

                for (int i = 0; i < extensions.Length; i++)
                {
                    formExtensionType= extensions.GetValue(i);

                    var info = formExtensionType.GetMethods(bindingFlags);

                    for (int J = 0; J < info .get_Length(); J++)
                    {
                        mInfoinfo .getValue(J);
                        if (mInfo.Name == _methodName)
                        {
                            return true;
                        }
                    }
                }
            }
        }
        catch (Exception::CLRError)
        {
            error(CLRInterop::getLastException().ToString());
        }

        return false;
    }

}



Private, Protected and Public attribute access in Class Extension


Private, Protected, and Public attribute access in Class Extension


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

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

In this demo, we show how to access all 3 access modifiers using the class extension.


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






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



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

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

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





following is the screenshot of the complete code of the 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 the info message.


Please feel free to contact me if you are facing any issues during the implementation of this blog.




Virtual Fields Vs Computed Fields

  Virtual Field: A virtual field in D365FO is a field that doesn't have a direct representation in the database. It's a field that y...