web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Delegates x++ interop .NET

(0) ShareShare
ReportReport
Posted on by

Hi all,

           I'd like to know if there is an easy way to map .NET delegates on X++. I know that X++ delegates don't return a value so this is not a viable use, but since they can be (and are) marshaled on .NET classes and used on X++ (though IL code) I'd like to know if somebody could show me an example.

 

More specifically I am trying to use tasks like the example below...

Task<int> task1 = new Task<int>(() =>  

{  

 int result = 1;  

          for (int i = 1; i < 10; i++)  

               result *= i;  

          return result;  

  });  

Anyway think this last code would be encapsulated on .NET code and what I must pass to .NET would be the X++ code.So that would be something like:

MyClassOnDotNetInteroped.PassADelegateFromXPlusPlus(MyDelegateOrHandlerOnXPlusPlus);

private int MyDelegateOrHandlerOnXPlusPlus()

{

}

How this could be achieved through X++?

Thanks and hope I explained properly  my problem.

 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,985 Most Valuable Professional on at

    AX 7 supports working with tasks, but there still no support for delegates (in usual sense) in X++ (not mentioning lambda functions), therefore you have to pass name of a static method (and its class) to be called. You could do something similar in AX 2012.

    Nevertheless if you can, move your code creating the task to a Visual Studio project. There you can use use proper .NET delegates and if you need something from AX, just use AX proxy classes.

  • Community Member Profile Picture
    on at

    Thanks so much! Indeed I managed to work.

  • Community Member Profile Picture
    on at

    BTW,

    Is there also a way to deal with generics on function calls? So for example how this function could be translated to X++? (where TMessage is the generic type)

    Handle<TMessage>(Func<TMessage, Task> handlerFunction)

    Thanks

  • Verified answer
    Martin Dráb Profile Picture
    237,985 Most Valuable Professional on at

    If my reply answered your question, please mark it as the verified answer. Otherwise please share your solution with us and mark that as the answer.

    Generics isn't supported by X++ either. You surely can assign a generic type to a variable of a compatible type, and you can even create instance of generic types via System.Activator (which is undeniably ugly). Again, my recommendation is wrapping these things in a .NET library and using C#, which does support generics. It's really easy thanks to VS projects in AOT.

  • Community Member Profile Picture
    on at

    Hi,

          I tried the code on X++ and seems to not work...Am In missing something, or actually you really cannot work with simple delegates? This is the example:

    public class A

    {

       System.Threading.Tasks.Task taskResult;

       public static void DoNothing()

       {

       }

       public System.Threading.Tasks.Task Handle()

       {

           taskResult = new System.Threading.Tasks.Task(DoNothing);

            return taskResult;

       }

    }

    Trying to pass the DoNothing function does not work (it does on C#), and no way with Static references on the function definition, class...

    Thanks so much....

    P.S.: I am a .NET developer and I am making a library to be consumed from X++, so X++ programmers can work with that, so moving code to X++ (for the X++ programmers is not an option...They are working with Dynamics 2012 R3).

    So if X++ interop is so limited on .NET I'd go on another way...

  • Verified answer
    Martin Dráb Profile Picture
    237,985 Most Valuable Professional on at

    As I said, there is no support for delegates (in CLR sense) in X++, therefore your code can't ever work. It depends on delegates and also on C# syntax for converting method groups to delegates. There is nothing like that in X++.

    Sorry, I should have explained the solution in better detail. If you get a class name and a method name, you can call the method via reflection. Then you could create .NET method doing this call and pass it to a task in C# code. If you want a generic solution, you may have a method that accepts a class/method to call (and possibly a callback class/method) and returns a task. That's exactly how runAsync() in AX7 works.

    I also suggested that you avoided that; you might be able to move the whole logic to your C# class library (avoiding calls from .NET to AX completely), or write a method that calls appropriate X++ objects through proxy classes, instead of building something generic with reflection.

    I didn't suggest to move your code to X++. I suggested exactly the opposite - stop trying to play with delegates and generics in X++ and do it in C#. You're writing a library to be used from X++, therefore you don't want to expose anything that can't be consumed there, therefore keep all such concepts hidden in your library and expose a simplified public interface. I use this approach myself.

  • epihodi Profile Picture
    31 on at

    Hello everyone.

    Maybe someone know some .Net library to call it from Ax and use HttpClient?

    Or each developer write it for himself?

  • Martin Dráb Profile Picture
    237,985 Most Valuable Professional on at

    Hi Anton, your question doesn't seem to be about delegates, therefore please create a new thread (with an appropriate title and tags) and we can discuss your problem there.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans