I am using .NET WebAPI to build a custom API. What I am looking to do is dynamically change a properties datamember name at runtime. I understand that I can override the name by using the DataMember(Name="whateverId") but I want to be able to dynamically change it.
See my example below. The class has an id and a name field. Sometimes I may want it to appear as "eventId" and "name". Other times I may want it to be "subEventId" and "name".
Any ideas how to do this dynamically.
[DataContract(Namespace = "", Name = "pair")]
public class idName
{
[DataMember(EmitDefaultValue = false]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public int? id { get; set; }
[DataMember(EmitDefaultValue = false)]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string name { get; set; }
public idName()
{
}
public idName(int? id, string name, string serializeIdName = "id")
{
this.id = id;
this.name = name;
}
}
I can create a new datamemberattribute in code but I don't know what to do with it next.
var dma = new DataMemberAttribute();
dma.Name = "whateverId";
Thanks!