karthik
karthik

Reputation: 135

Getting the name of [DataMember] in C#

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?

Upvotes: 10

Views: 14853

Answers (4)

user566399
user566399

Reputation: 19

public static class TypeExtensions
{

    /// <summary>
    /// Returns the nameofMember's DataMemberAttribute.Name
    /// Example usage:
    /// var aPropertyDataMemberName = typeof(MyType).GetDataMemberName(nameof(MyType.AProperty));
    /// </summary>
    /// <param name="type">The type. Example: typeof(MyType)</param>
    /// <param name="nameofMember">The member of the type. Example: nameof(MyType.AProperty)</param>
    /// <param name="throwIfMemberDoesntExist">Optionally throw if the member does not exist</param>
    /// <returns>nameofMember's DataMemberAttribute.Name
    /// </returns>
    /// <exception cref="ArgumentOutOfRangeException">Thrown if the member does not exist</exception>
    public static string? GetDataMemberName(this Type type, string nameofMember, bool throwIfMemberDoesNotExist = true)
    {
        MemberInfo? memberInfo = type?.GetProperty(nameofMember) ?? type?.GetField(nameofMember) ?? type?.GetMember(nameofMember)?.FirstOrDefault();
        string? ret = (memberInfo?.GetCustomAttribute(typeof(DataMemberAttribute), true) as DataMemberAttribute)?.Name ?? string.Empty;
        if (memberInfo == null && throwIfMemberDoesNotExist) { throw new ArgumentOutOfRangeException(nameof(nameofMember), "Member does not exist."); }
        return ret;
    }
}

Upvotes: 0

brewmanz
brewmanz

Reputation: 1221

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

Upvotes: 3

Antonio Petricca
Antonio Petricca

Reputation: 10996

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.

Upvotes: 1

Ehsan
Ehsan

Reputation: 32651

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

Upvotes: 16

Related Questions