1

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!

jrock10
  • 388
  • 1
  • 4
  • 12
  • The following post may provide some suggestions for your problem: http://stackoverflow.com/questions/6665187/how-to-set-dynamic-value-in-my-attribute – David Tansey May 10 '13 at 16:34

2 Answers2

0

You can use serialization callback, like so:


[DataContract]
public class IdName
{
    public int? Id { get; set; }

[DataMember(EmitDefaultValue = false)]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
private string EventId { get; set; }

[DataMember(EmitDefaultValue = false)]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
private string SubEventId { get; set; }

[DataMember(EmitDefaultValue = false)]
public string Name { get; set; }

[OnSerializing]
void OnSerializing(StreamingContext context)
{
    bool isEvent = true;
    if (isEvent)
    {
        this.EventId = this.Id.ToString();
    }
    else
    {
        this.SubEventId = this.Id.ToString();
    }
}

}

  • How would this work if I wanted to make a call like this: var oId = new idName(5,"test","nameToSerializeAs"). I really am looking to just pass a name into the constructor and have it serialize as that name. – jrock10 May 13 '13 at 14:57
  • Okay, passing the name is a new requirement. Your original post asks for ID to be serialized sometimes as EventID and other times as SubeventID and that should be taken care off by the code above. – Badrinarayanan Lakshmiraghavan May 13 '13 at 16:23
  • Your post did help think of an alternate solution. Not exactly dynamic but better than having duplicate objects. – jrock10 May 13 '13 at 18:28
0

Here is my less than ideal but improved approach.

public class idName
{

    [DataMember(EmitDefaultValue = false)]
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? id { get; set; }

    #region Alternate id names

    [DataMember(EmitDefaultValue = false)]
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? eventId { get; set; }

    [DataMember(EmitDefaultValue = false)]
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? subEventId { get; set; }

    //as many other id's as needed.

    #endregion

    [DataMember(EmitDefaultValue = false)]
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string name { get; set; }

    public idName() { }

    public idName(int? id, string name, string displayIdName = "id")
    {
        this.name = name;

        PropertyInfo prop = this.GetType().GetProperty(displayIdName);
        if (prop == null)
            this.id = id;
        else
            prop.SetValue(this, id);

    }
}
jrock10
  • 388
  • 1
  • 4
  • 12