DEmos from the .NET Interop for Mere Mortals

  • Post category:Development
  • Post comments:14 Comments
  • Reading time:1 mins read

Thanks to everybody who attended my session today at NAV TechDays 2014 in Antwerp. As promised, here are the goodies from the session. I’ve simply taken the objects and I deliver them here with no explanations or comments – when Luc publishes the sessions and the slide decks on Mibuso, you’ll be able to figure out what’s what. I hope you don’t mind.

I’ve also included my demo from the PRS session today, about the Service Locator pattern. The ZIP file does not include the Visual Studio solution – you can find it in my previous post, that I’ve put here yesterday.

I hope you find these examples useful and that they help you get started with .NET Interop quickly.

Download the files here: https://vjeko.com/wp-content/uploads/2014/11/NAVTechDays2014.MereMortals.zip.

Vjeko

Vjeko has been writing code for living since 1995, and he has shared his knowledge and experience in presentations, articles, blogs, and elsewhere since 2002. Hopelessly curious, passionate about technology, avid language learner no matter human or computer.

This Post Has 14 Comments

  1. Matthias König

    Hi Vjeko,

    the session was great 🙂 And thanks for sharing all the stuff with us 🙂 thats really helpfull to remember all the things we heard over the 2 days.
    But I’ve one question: Now I’ve played with the String and Array functions and getting an error message while I use the overloaded split method:
    ArrayOfResults := String.Split(ArrayOfDelimiters, StringSplitOptions.RemoveEmptyEntries);

    The error message is the following:
    The call is ambiguous between the following methods:
    ‘Split(string[] separator, StringSplitOptions options)’
    and
    ‘Split(char[] separator, StringSplitOptions options)’

    I thought to use ArrayOfDelimiters.toString() could help but it didn’t. How to handle such an error?

    1. Vjeko

      @Matthias: thanks for the feedback, I really appreciate it. Regarding the Split function (or many other methods which fail at resolving the exact method at runtime) the trick is to use reflection and get the instance of MethodInfo and then call MethodInfo.Invoke. I can’t test it right now, but the code would go somewhat like this:
      Type := GETDOTNETTYPE(String);
      MethodInfo := Type.GetMethod(‘Split’,ArrayOfTypes);
      MethodInfo.Invoke(String,ArrayOfValues);
      The ArrayOfTypes has to have two elements, first of ArrayOfString.GetType(), and second of GETDOTNETTYPE(StringSplitOptios), and ArrayOfValues would have to have those two parameters that you attempt to pass directly to String.Split.

      I hope this helps.

      1. Matthias König

        thanks for the fast reply. But I feel very “newby” because I do not know how to create an ArrayOfTypes with different Types 🙁

        I tried an array of obects and several CreateInstances.
        Error Message: “a call to System.RuntimeType.GetMethod failed with this message: the type of one or more arguments does not match the methods parameter type.”

        did you give a hint?

        CurrentCode:

        Type := GETDOTNETTYPE(String);
        ArrayOfTypes := ArrayOfDelimiters.CreateInstance(Type, 2);
        MethodInfo := Type.GetMethod(‘Split’, ArrayOfTypes);

        sorry for that annoying questions :/

        1. Matthias König

          can not edit my comment. I did not want to sound harsh :). Want to say “did you have a hint for me?”. If it is to special, its okay too,

        2. Vjeko

          No worries, Matthias – you are not harsh 🙂

          Anyway, after the ArrayOfTypes instantiation, you need to call ArrayOfTypes.SetValue twice for each of the types of the Split method parameters. I have no time to write this now, but I definitely will blog about this and other methods inaccessible due to overloading with types that C/AL treats as same.

          1. Matthias König

            Hi,

            now I could have a try but for another overloaded function: remove on String. There are two overloaded methods. One with charater and another with strings. Here my Code snippet:

            ArrayOfTypes := ArrayOfTypes.CreateInstance(GETDOTNETTYPE(Type), 2);

            Type := GETDOTNETTYPE(String);
            ArrayOfTypes.SetValue(GETDOTNETTYPE(String), 0);
            ArrayOfTypes.SetValue(GETDOTNETTYPE(String), 1);
            MethodInfo := Type.GetMethod(‘Replace’, ArrayOfTypes);

            ArrayOfValues := ArrayOfValues.CreateInstance(GETDOTNETTYPE(String), 2);

            ArrayOfValues.SetValue(‘Hello’, 0); // from Hello to hallo
            ArrayOfValues.SetValue(‘Hallo’, 1);

            MESSAGE(FORMAT(MethodInfo.Invoke(String, ArrayOfValues))); // in the String variable is my Text in which I want to replace the words

            thanks 🙂

  2. Oliver Kunz

    Hello Vjeko,
    thanks for the great sessions on navtechdays – very helpful and inspiringly.
    It would be nice if you can publish the MereMortals-Objects also as .txt because my 2013 DevEnv isn’t able to import the fob objects.
    Best wishes
    Oki

    1. Vjeko

      Hi Oliver, thanks for the comments! I can’t promise to do this, as I have already cleaned up my machine and the objects now exist only as FOB files on my blog. If you don’t have the NAV 2015 environment to import the FOBs there and export them as text, then please let me know and I’ll see what I can do.

      1. Oliver Kunz

        Hi Vjeko, I’ve setup NAV2015 and imported the objects. No need to publish the text export anymore.
        Long life and peace…
        Oki

        1. Vjeko

          Sure 🙂 Same to you 🙂

  3. Tim Simmonds

    Just to say a big thanks for your contribution to the NavTechDays. Excellent info and tips on NAV Client Add-ons…well presented. Now I just have to practice it! Cheers.

    1. Vjeko

      Thanks, Tim! I really appreciate it.

  4. Tobias Bäuerle

    Hi Vjeko, I visited your sessions at the TechDays2014 and I was really impressed. Thanks for all the useful tipps and tricks 😉 I downloaded the presented demos from this blog but I am missing the demo where you created the DLL in-memory from within C/AL code. I would like to see that example because I think it could be useful in many projects. Thx

Leave a Reply