根据方法更改数据成员名称
[DataContract()]
public class Contract
{
.........
Some Properties with DataMembers Attribute.
............
..............
[DataMember(Name = "FirstName")]
public string Name { get; set; }
}
我有上面的类,并且有两个 Web 方法(操作),
在第一个 Web 方法中,我想将名称公开为 FirstName,在第二个 Web 方法中将名称公开为 LastName。
我不想创建单独的数据合同。
请告诉我如何实现这一目标?
[DataContract()]
public class Contract
{
.........
Some Properties with DataMembers Attribute.
............
..............
[DataMember(Name = "FirstName")]
public string Name { get; set; }
}
I have above class and I have two web methods (operations)
In 1st web method I want to expose Name as FirstName and in 2nd web method expose Name as LastName.
I don't want to create separate data contracts.
Please tell me how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用反射并在运行时更改属性的值,具体取决于您当前使用的方法。查看 StackOverflow 上的这些链接:
如何使用反射设置属性值
在运行时更改属性的参数
但我认为最好的方法是有两个单独的合同或创建具有这两个属性的合同。
You can use reflection and change the value of the attribute at runtime depending on which method you in currently. Check out these links on StackOverflow:
How to set attributes values using reflection
Change Attribute's parameter at runtime
But I think the best way would be to either have 2 separate contracts or create a contract with both properties.
您还可以在序列化期间实现代理以特例此类型。例如,请参阅数据合同代理示例。
You can also implement a surrogate to special-case this type during serialization. As an example, see the Data Contract Surrogate sample.