I have the model class like the following,
public class Station
{
[DataMember (Name="stationName")]
public string StationName;
[DataMember (Name="stationId")]
public string StationId;
}
I would like to get the Name of DataMember
with Property Name, i.e If I have the property name "StationName", How can I get the stationName
?
A slight modification to your class
[DataContract]
public class Station
{
[DataMember(Name = "stationName")]
public string StationName { get; set; }
[DataMember(Name = "stationId")]
public string StationId { get; set; }
}
and then this is how you can get it
var properties = typeof(Station).GetProperties();
foreach (var property in properties)
{
var attributes = property.GetCustomAttributes(typeof(DataMemberAttribute), true);
foreach (DataMemberAttribute dma in attributes)
{
Console.WriteLine(dma.Name);
}
}