Problem with Invoke method in extension
Question

Hi,

I try to create extension to call Action by it's name from the set with the same input parameter. I found discussion https://www.outsystems.com/forums/discussion/59902/call-actions-from-a-module-from-an-extension/ and tried extension https://www.outsystems.com/forge/component-overview/3283/action-reflection to have a starting point but it didn't work.

I tried to find issue and separated this row

actionsType.InvokeMember(string.Format("Action{0}", ssactionName), 
      BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, 
      null, null, new object[] { contextInstance });

into two rows

MethodInfo myMethod = actionsType.GetMethod(string.Format("Action{0}", ssactionName),
      BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public);
myMethod.Invoke(null, new object[] { contextInstance });

When I put a breakpoint after the first of this two rows, I see my method, so until then everything is correct. But after next step I get the error 

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.

But I found in the documentation https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.invoke?view=netframework-1.1 that

Thanks.

Solution

I had same issue. Resolved with below code. 


Try below code.   

  public object GetHeContext()

        {

            return AppInfo.GetAppInfo().OsContext;

        }

var context = GetHeContext(); 

 actionsType.InvokeMember($"Action{ssActionName}",

 BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,

 null, null, new[] { context });


Velkumar, thank you very much! It's worked!

mvp_badge
MVP

Hello Daryna,

What exactly is your use case? Is it plain experimentation or something else?

João Heleno wrote:

Hello Daryna,

What exactly is your use case? Is it plain experimentation or something else?

 Hi João,

I want to optimize action with switch that will be very big in the future. I have set of actions that are different inside but have similar names and the same input parameters and I want to choose the next action according to the incoming data.

 

Wow, this can be done? Thank you for asking this question because I would never have found this behavior. It opens door that can solve a major business case for us!

mvp_badge
MVP

I'm still thinking about this one...

What everyone should realize is that this relies on undocumented Platform features that can suddenly change/disappear.

Yes, it was undocumented OutSystems feature but it looks strange that the result of calling the method in the extension is inconsistent with official .Net documentation now.

Hi Daryna, 

Did you managed to succeed in calling actions with parameters dynamically? I am also trying to accomplish it but have not any luck yet.


Hi Romano,

Unfortunately, I didn't manage to do it. So the question is still open.

Hi,

I'm facing the same problem.

Any ideas?


Thank you

Solution

I had same issue. Resolved with below code. 


Try below code.   

  public object GetHeContext()

        {

            return AppInfo.GetAppInfo().OsContext;

        }

var context = GetHeContext(); 

 actionsType.InvokeMember($"Action{ssActionName}",

 BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,

 null, null, new[] { context });


Velkumar, thank you very much! It's worked!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.