Monday, August 13, 2018

X++ | d365FO | reflection - GetField not return a value

I found that method GetField not return a value as expected in some case, see below figure. The problem is on line 9, the variable 'field' get a null value return, instead of custConfirmJour.



























Solution
The reason is still unclear to me. But to solve it in very simple way, just declare all type of bindingFlags as follows.

using System.Reflection;

[ExtensionOf(classStr(SalesConfirmController))]
final class SalesConfirmController_XXX_Extension
{
    Common XXX_ABC()
    {
        //var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic;
        var bindFlags = BindingFlags::Instance | 
BindingFlags::NonPublic |
                        BindingFlags::Static | 
                        BindingFlags::Public;
        var field = this.GetType().GetField("custConfirmJour", bindFlags);
        Common ret = field.GetValue(this);

        return ret;
    }

}

Until the next post!


References:
https://stackoverflow.com/questions/1040803/whats-wrong-with-this-reflection-code-getfields-is-returning-an-empty-array
https://msdn.microsoft.com/en-us/library/4ek9c21e(v=vs.110).aspx

No comments:

Post a Comment