[SOLVED] Getting the name of all DataMembers in a DataContract

Issue

I am using 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 the DataMembers in this class CustomBranches

Like “ID” , “branch name” etc

Thanks

Solution

Seeing as it’s a data-contract you should most likely have all these properties available, but if not the following should list all properties of your type:

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

replace T with the type, and you’re good to go. Depending on your needs you might want different BindingFlags.

Answered By – nillls

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *