Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Consuming C# classes from the Collections.Generic namespace

Posted on by 541

I'm writing some code related to service bus in X++.

In part of my code I need to read BrokeredMessage.Properties (https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.brokeredmessage.properties?view=azure-dotnet) which is of type System.Collections.Generic.IDictionary<string,object>

Within X++ I can reference this as System.Collections.Generic.IDictionary`2 however the ` creates a compilation error.

Any idea how I can go about this?  I currently am using var, but then I do not get the intellisense.

 

  • nspiteri Profile Picture
    nspiteri 541 on at
    RE: Consuming C# classes from the Collections.Generic namespace

    Hi Martin,

    you're absolutely right about this.  The error does occur at compile time.  I was under the wrong impression that since Intellisense half works (some methods show up while others don't), that var, in Dynamics, was choosing a more generic type.

    Thanks for pointing that out.  I think its another note of improvement for the guys working on the IDE.

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Consuming C# classes from the Collections.Generic namespace

    That's not correct - var doesn't mean weakly typing (as with anytype or so) and you'll get compile-time checking. It's true that IntelliSense often have a problem with it. Using var means that the type is detected by compiler, not that the type is unknown. Could you test it once more, please? For example, try to compile this:

    var properties = message.Properties;
    properties.Abcdefg();

    The compiler should tell you that no such a method exists.

    I'm not saying that you can't work with generic collections in older versions, but you can't do it in this way. My recommendation is using non-generic interfaces  there, such as IEnumerable if you want to iterate the collection. You can also declare the type is CLRObject, but then you really don't get any compile-time checking.

  • nspiteri Profile Picture
    nspiteri 541 on at
    RE: Consuming C# classes from the Collections.Generic namespace

    Thanks for the information.

    I suspected it couldn't be done in older versions but I wanted confirmation.

    Regarding F&O, as I mentioned, I have successfully used var but it makes it an object type so I don't get Intellisense or compile time checks.. I am interested to know if there is an alternative.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Consuming C# classes from the Collections.Generic namespace

    In F&O, simply use var as the type name:

    var properties = message.Properties;

    But you can't do that in older versions. If you need a solution for F&O, we're done. If you need a solutions for older versions, this is completely useless to you and we'll need to discuss other ways.

  • nspiteri Profile Picture
    nspiteri 541 on at
    RE: Consuming C# classes from the Collections.Generic namespace

    I left the version part open on purpose as I'm curious how it would be done both in F&O and in Ax.  Most importantly in F&O though.

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Consuming C# classes from the Collections.Generic namespace

    Hi Normbert, I moved your question from Dynamics 365 General Forum here to the AX forum, but I don't which version you're talking about and whether it shouldn't actually be in an F&O forum. Please clarify.

    Your options depend on the version.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans