Quantcast
Viewing all articles
Browse latest Browse all 48

‘is’ and ‘as’ operators with .NET types

I recently ran into an unfortunate limitation of .NET Interop from X++ (in D365FO).
I wanted to check if an X++ object is of a given type, nevertheless the type used for the variable declaration was a .NET interface. Here is an example:

using Microsoft.Dynamics.ApplicationSuite.FinancialManagement.Currency.Framework;
 
void demo(IExchangeRateProvider _provider)
{
    if (_provider is ExchangeRateProviderCBOE) {}
}

Exchange rate providers are X++ classing implementing IExchangeRateProvider interface and I wanted to check if the object I received was a particular provider (namely ExchangeRateProviderCBOE class or its child). Unfortunately this ended up with a compilation error:

The operand on the left side of the ‘is’ or ‘as’ operator must be a table, class, or form.

As I tested, neither managed interfaces nor managed classes (such as System.Object) can be used on the left side of ‘is’ and ‘as’ operators.

I solved the problem by using Type.IsAssignableFrom() method. Simply using the ‘is’ operator would be nicer, but this does the job too.

void demo(IExchangeRateProvider _provider)
{
    System.Object providerObj = _provider;
    if (providerObj.GetType().IsAssignableFrom(new ExchangeRateProviderCBOE().GetType())) {}
}

Note that the problem is only with the left side of the operators, e.g. when you want to check whether a managed type is this or that. Using the ‘is’ operator to check if an instance of an X++ class implements a managed interface works without problems.

Object obj = new ExchangeRateProviderCBOE();
if (obj is IExchangeRateProvider) {}

Viewing all articles
Browse latest Browse all 48

Trending Articles


Re: Prosím o určení autora - google nepomáhá


Nelze se přihlásit na Facebook přes PC


Prodám Flexi pass - 3 200


Markéta Reinischová: Chceme s Filipem Jankovičem dítě!


Od: Martina


Podzemlje - epizoda 62


Defender


Plynový kotel DAKON DS 22G - 2 500


Gymnastické řemínky na hrazdu zn. Reisport, vel. č. 2: 590


P: NooK Soundelirium THE 12.6


Qube SP26 ( XTA DP226 ) signal processor - 12 000


Narovnání,vylisování bankovek


Javorina Holubyho chata


Tinylab: Tlačítka


Levasan Maxx není gel na klouby, nýbrž hnus


RNS315 couvací kamera


Kde najdu GameInput Service ve win 10?


Redmi Note 11 Pro+ 5G (PISSARO)


MV3 Vermona, Klingenthal, NDR


Hradcany 30h fialova razena 11 1/2 11 3/4