Getting the name of [DataMember] in C#

datamember property c#
c# datamember(name not working)
c datamember(order)
datamember vs jsonproperty
c datamember default value
c# get property description attribute
c# datacontract serialization

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);
      }                
  }

DataMemberAttribute.Name Property (System.Runtime.Serialization , By default it is false. [DataMember(IsRequired=true)]; public int StudentId; {; get { return Id; } set { Id = value; }; }. Name: By using this property we  How can I reflectively get the property that has the DataMember with a given name (let's assume every DataMember has a unique name)? For example, in the following code the property with the DataMember that has name "p1" is PropertyOne:


You can do it simply by using the Reflection, cast that return attribute to DataMemberAttribute class and read the Name property value.

Here is a complete 3rd party example.

DataMemberAttribute Class (System.Runtime.Serialization , [DataContract] public class OptimizationNodeDto : IModelWithId { [DataMember] public int Id { get; set; } [DataMember(Name = "parentId")]  By default, the CLR member name is used as the name of the data member. By setting the Name property, you can customize the name of the data member. This can be used to provide a name that may not be allowed as a CLR member name. When mapping to XML using the DataContractSerializer, this name is used as the name of the schema element in a type.


I created an extension method:

        public static string GetDataMemberName(this MyClass theClass, string thePropertyName)
    {
        var pi = typeof(MyClass).GetProperty(thePropertyName);
        if (pi == null) throw new ApplicationException($"{nameof(MyClass)}.{thePropertyName} does not exist");
        var ca = pi.GetCustomAttribute(typeof(DataMemberAttribute), true) as DataMemberAttribute;
        if (ca == null) throw new ApplicationException($"{nameof(MyClass)}.{thePropertyName} does not have DataMember Attribute"); // or return thePropertyName?
        return ca.Name;
    }

with usage

myInstance.GetDataMemberName(nameof(MyClass.MyPropertyName)))

Data Contract Names, I prefer Pascal casing on my object properties in C#, so I wanted to quickly get; set; } } [DataContract] public class Feed { [DataMember(Name  this is great for dealing with getting a variable name. how about getting Name in parameter? thanks in advance – olidev Mar 21 '12 at 9:51 1 @devn I've edited my answer with a sample code that shows how to use the same method to get a name of parameter.


Data Member Attribute And Its Parameters in WCF, public string Result { get; set; } is an odd thing to do in C# code. Unfortunately the JSON serializer doesn't support the [DataMember(Name=)] attribute (it's  [DataMember] has some parameters, which specify the details about Data Member. The name of these parameters and their functions are given below. EmitDefaultValue This parameter specifies that whether or not, you are required to serialize the default value of a data member. Its default value is true. Example


What a DataContract and DataMember Are in WCF: Part 5, [DataContract] public class File { // excluded from serialization // does not have DataMemberAttribute public Guid Id { get; set; } [DataMember] public string Name​  Examples. The following code example illustrates a common use for this property. In the example, a DataGridView control named customersDataGridView is bound to a DataSet named customersDataSet.


[SOLVED], public string Name { get; set; } member this.Name : string with get, set Public Property Name As String Property Value String. The name of the data member. The default is the name of the target that the attribute is applied to. Remarks. This property is used to override the default name of the data member that is derived from the name of the