通过WCF传递匿名类型的实例 [英] Passing an instance of anonymous type over WCF

查看:66
本文介绍了通过WCF传递匿名类型的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务方法,该方法需要一个对象,然后使用反射检索其属性.

I have a WCF service method that expects an object and then retrieves its properties using reflection.

在客户端,我创建一个匿名类型对象

On the client side I create an anonymous type object

var obj = new {FirstName="John", LastName="Doe"}

并将其传递给方法.我有一个例外:

and pass it to the method. I'm getting an exception:

Type '<>f__AnonymousType0`2[System.String,System.String]' cannot be serialized. 
Consider marking it with the DataContractAttribute attribute, and marking all 
of its members you want serialized with the DataMemberAttribute attribute.  
See the Microsoft .NET Framework documentation for other supported types.

我无法使用序列化属性标记类型或其成员,因为实际上没有显式声明的类型或属性.有办法解决这个问题吗?

I can't mark the type or its members with serialization attributes because there is really no type nor properties declared explicitly. Is there a way to overcome that problem?

推荐答案

请勿执行此操作.

这是一个聪明的尝试.别.只需声明所需的数据类型,然后使用它即可.如果您需要更宽松定义的数据类型,则只需使用某种键值映射即可.

It's an attempt to be clever. Don't. Just declare the datatype you need, and use it. If you need a more loosely-defined datatype, just use a key-value mapping of some sort.

写一些可以永久解决的事情可能需要5分钟.使用类似这样的技术将花费您数小时的调试时间.

It will take you 5 minutes to write something that can handle this for good. Using any technique like this will cost you hours of debugging at some future point down the road.

这篇关于通过WCF传递匿名类型的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆