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

**URL:** <https://www.dynamicsuser.net/t/getting-error-microsoft-dynamics-anytype-cannot-be-marshaled-to-clr-object/66857>\
**Category:** User Forum\
**Created:** [March 20, 2019, 3:31am UTC](https://www.dynamicsuser.net/t/getting-error-microsoft-dynamics-anytype-cannot-be-marshaled-to-clr-object/66857 "2019-03-20T03:31:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FormerMember](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FormerMember](https://www.dynamicsuser.net/u/FormerMember)\
**Post date:** [March 20, 2019, 3:31am UTC](https://www.dynamicsuser.net/t/getting-error-microsoft-dynamics-anytype-cannot-be-marshaled-to-clr-object/66857/1 "2019-03-20T03:31:45Z")

</div>

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:

[DataContractAttribute]  
public class DzungXmlContract  
{  
CustAccount custAccount;  
Name name;  
}

[DataMemberAttribute(“CustomerAccount”)]  
public CustAccount parmCustAccount(CustAccount \_custAccount = custAccount)  
{  
custAccount = \_custAccount;

return custAccount;  
}

[DataMemberAttribute(“Name”)]  
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);  
permission.assert();

contract = new DzungXmlContract();  
contract.parmName(“Test”);  
contract.parmCustAccount(“ABC123”);

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

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

CodeAccessPermission::revertAssert();

return serialized;  
}

The job:

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

result = test.testXml();

info(result);  
}

---

<div class="post-metadata">

**Author:** ![MartinDrab](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@MartinDrab](https://www.dynamicsuser.net/u/MartinDrab)\
**Post date:** [March 20, 2019, 5:12am UTC](https://www.dynamicsuser.net/t/getting-error-microsoft-dynamics-anytype-cannot-be-marshaled-to-clr-object/66857/2 "2019-03-20T05:12:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FormerMember](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FormerMember](https://www.dynamicsuser.net/u/FormerMember)\
**Post date:** [March 20, 2019, 6:55am UTC](https://www.dynamicsuser.net/t/getting-error-microsoft-dynamics-anytype-cannot-be-marshaled-to-clr-object/66857/3 "2019-03-20T06:55:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MartinDrab](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@MartinDrab](https://www.dynamicsuser.net/u/MartinDrab)\
**Post date:** [March 20, 2019, 1:57pm UTC](https://www.dynamicsuser.net/t/getting-error-microsoft-dynamics-anytype-cannot-be-marshaled-to-clr-object/66857/4 "2019-03-20T13:57:28Z")

</div>

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++](http://dev.goshoom.net/en/2014/01/data-contract-serialization/), where I explained these things with a bit more details.

---

<div class="post-metadata">

**Author:** ![FormerMember](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FormerMember](https://www.dynamicsuser.net/u/FormerMember)\
**Post date:** [March 21, 2019, 2:12am UTC](https://www.dynamicsuser.net/t/getting-error-microsoft-dynamics-anytype-cannot-be-marshaled-to-clr-object/66857/5 "2019-03-21T02:12:46Z")

</div>

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