Getting the name of all DataMembers in a DataContract

I am using the WCF service

I have a data contract:

[DataContract] [KnownType(typeof(CustomBranches))] public class CustomBranches { [DataMember] public int Id { get; set; } [DataMember] public string branch_name { get; set; } [DataMember] public string address_line_1 { get; set; } [DataMember] public string city_name { get; set; } } 

Is it possible that I can find the name of all DataMembers of this CustomBranches class

Like "ID", "branch name", etc.

thanks

+7
source share
3 answers

When you see that this is a data contract, you should most likely have all these properties, but if not the following, list all the properties of your type:

 Type T =(typeof(T)); var properties = T.GetProperties(BindingFlags.Public|BindingFlags.Instance); 

replace T with type and you are good to go. Depending on your needs, you may need another BindingFlags .

+4
source

What you need to do:

  • You do not need to add [KnownType(typeof(CustomBranches))] to your CustomBranches class. The class always knows about itself.
  • You need to filter the properties to get only those that have the [DataMember] (nillls code returned all of them)
  • Data item properties can also be non-public (it works if serialization works in full)
  • Data items can also be fields (not just properties), so you also need to consider them.

This is an example of code that makes all of them.

 public class StackOverflow_8152252 { public static void Test() { BindingFlags instancePublicAndNot = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; var memberNames = typeof(CustomBranches) .GetProperties(instancePublicAndNot) .OfType<MemberInfo>() .Union(typeof(CustomBranches).GetFields(instancePublicAndNot)) .Where(x => Attribute.IsDefined(x, typeof(DataMemberAttribute))) .Select(x => x.Name); Console.WriteLine("All data member names"); foreach (var memberName in memberNames) { Console.WriteLine(" {0}", memberName); } } [DataContract] public class CustomBranches { [DataMember] public int Id { get; set; } [DataMember] public string branch_name { get; set; } [DataMember] public string address_line_1 { get; set; } [DataMember] public string city_name { get; set; } public int NonDataMember { get; set; } [DataMember] public string FieldDataMember; [DataMember] internal string NonPublicMember { get; set; } } } 
+14
source

I modified Carlos' answer a bit to ignore data items decorated with the [NonSerialized] attribute:

 public class StackOverflow_8152252 { public static void Test() { BindingFlags instancePublicAndNot = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; var memberNames = typeof(CustomBranches) .GetProperties(instancePublicAndNot) .OfType<MemberInfo>() .Union(typeof(CustomBranches).GetFields(instancePublicAndNot)) .Where(x => Attribute.IsDefined(x, typeof(DataMemberAttribute)) && !Attribute.IsDefined(x, typeof(NonSerializedAttribute))) .Select(x => x.Name); Console.WriteLine("All data member names"); foreach (var memberName in memberNames) { Console.WriteLine(" {0}", memberName); } } [DataContract] public class CustomBranches { [DataMember] public int Id { get; set; } [DataMember] [NonSerialized] public string NonSerializedDataMember { get; set; } } } 
+3
source

All Articles