.Net Interop: Using the Parent to get to the Child (Using Enumerations Part 2)

If you haven't read the first part of this article, I recommend you do so now.

It's been a while ago since I promised you the second part of the article. Sorry it took this long .. As you noticed .. there were other things to talk about ;°).

We are trying to use enumerations in a more attractive way than writing something like this:

enumBasicHttpSecurityMode.Parse(enumBasicHttpSecurityMode.GetType(), 'TransportCredentialOnly');

Parsing .. GetType .. not all that logical if you're used to C/AL. Not simple .. and we're all into "simplicity", right? Smile

So, together with my colleague Bart (.. the ReVision-guy) we worked on a small solution.. .

Let's try to make a function, that works for all enumerations. To do this, we have to know what all enumerations have in common .. and we can use. Well, all enums derive from System.Enum. So what if .. we can create a function that can handle any enum (in any namespace, class, or whatever), and gives back the integer value .. ? It's perfectly possible, and very easy .. but is it simple? This is our function:

Enum2Int(pSystemEnum : DotNet "'mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Enum";pEnumValue : Text(30)) : Integer


I hope it's readable for you .. . The main trick is using a System.Enum, which can hold any enumeration. Then, I parse the type of the enum that was sent by the parameter, and convert the result to an integer with the System.Convert (in the mscorlib namespace). One line of code in the function, but now I can convert the code from Part 1 to something like this:

navBinding := navBinding.BasicHttpBinding;

// Set security mode to BasicHttpSecurityMode.TransportCredentialOnly
navBinding.Security.Mode := Enum2Int(navBinding.Security.Mode, 'TransportCredentialOnly');

address :=
'http://localhost:7047/DynamicsNAV/WS/Second Company/Page/Customers';

// Set client credential type to HttpClientCredentialType.Windows
navBinding.Security.Transport.ClientCredentialType := Enum2Int(navBinding.Security.Transport.ClientCredentialType,'Windows');

CustomerService := CustomerService.Customers_PortClient(navBinding, endpointAddress.EndpointAddress(address));

// Set impersonation level to System.Security.Principal.TokenImpersonationLevel.Delegation
CustomerService.ClientCredentials.Windows.AllowedImpersonationLevel :=

Enum2Int(CustomerService.ClientCredentials.Windows.AllowedImpersonationLevel, 'Delegation');

// Include the sales order ID to be read.
Customer := CustomerService.Read('10000');


Much much more readable, and closest I can come to the c#-style of using enums.. .

This is 'simplicity', and it was made possible to think more in a ".Net"-way: using parent classes to make "general" functions, which can be used by all child-classes.. :-) .. or at least something in that area.

Hope you like it!

Comment List