Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kbrammer/5189326 to your computer and use it in GitHub Desktop.
Save kbrammer/5189326 to your computer and use it in GitHub Desktop.
Return DataMemberAttribute names for a DataContract object or property
//Returns DataMemberAttribute names for a DataContract object
public static List<string> GetDataMemberAttributeNames(object obj)
{
List<string> result = new List<string>();
foreach (PropertyInfo p in obj.GetType().GetProperties())
{
foreach (System.Runtime.Serialization.DataMemberAttribute ca in p.GetCustomAttributes(false))
{
result.Add((string)ca.Name);
}
}
return result;
}
//Returns the DataMemberAttribute name for a DataContract property
public static string GetDataMemberAttributeName(PropertyInfo p)
{
string result = "";
foreach (System.Runtime.Serialization.DataMemberAttribute ca in p.GetCustomAttributes(false))
{
result = (string)ca.Name;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment