0
 [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?

meetjaydeep
  • 1,752
  • 4
  • 25
  • 39

2 Answers2

0

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.

Community
  • 1
  • 1
Sergio Vicente
  • 980
  • 1
  • 10
  • 18
0

You can also implement a surrogate to special-case this type during serialization. As an example, see the Data Contract Surrogate sample.

krisragh MSFT
  • 1,908
  • 1
  • 13
  • 22