Getting error: Microsoft Dynamics anytype cannot be marshaled to CLR Object

Hi all,

I’m calling the function CLRInterop::getObjectForAnyType() and kept getting the error “Microsoft Dynamics anytype cannot be marshaled to CLR Object”. I don’t know what I’m doing wrong here so anyone out there knows please help, thanks. Both classes are set to run on server. I’m using on-prem Dynamics AX 2012 R3.

Below are my code:

The contract class:

public class DzungXmlContract
CustAccount custAccount;
Name name;

public CustAccount parmCustAccount(CustAccount _custAccount = custAccount)
custAccount = _custAccount;

return custAccount;

public Name parmName(Name _name = name)
name = _name;

return name;

The business logic class:

public class DzungXmlTestClass

public str testXml()
System.Object clrContract;
str serialized;
InteropPermission permission;
DzungXmlContract contract;

permission = new InteropPermission(InteropKind::ClrInterop);

contract = new DzungXmlContract();

clrContract = CLRInterop::getObjectForAnyType(contract); // This line throws the error.

serialized = ContractSerializer::Serialize(clrContract); // .Net xml serializer.


return serialized;

The job:

static void DzungXmlTestJob(Args _args)
str result;
DzungXmlTestClass test = new DzungXmlTestClass();

result = test.testXml();


What .NET object do you expect that the X++ class will be converted into? The type interoperability is designed for primitive types such as string.

Consider creating DzungXmlContract as a C# class instead of an X++ class.

Hi Martin,

Thanks for responding. I would expect the class to be converted into CLR equivalent proxy class of DzungXmlContract which would have been generated when I did the “Generate Full CIL”, am I correct.

Anyway, I’ve resolved the issue. It seems to me that the CLRInterop::getObjectForAnyType only work in class that used by an AX webservice or in a batch job. Executing a job/class (that calls CLRInterop::getObjectForAnyType) directly from the AX client will results in the error.

Web services and batch jobs run in CIL, therefore there is no conversion from X++ to CIL - everything is already in CIL.

Note that CIL generated from X++ exists only on AOS. If you run code on client; the CIL code you want to use doesn’t exist there, therefore it can’t work even theoretically.

Also note that the class isn’t a proxy - it’s the same class compiled to CIL.

You may be interested in my blog post Data contract serialization from X++, where I explained these things with a bit more details.

Thanks for the explanation and yes I did read your blog, very helpful.