Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the name of all DataMembers in a DataContract

Tags:

c#

asp.net

wcf

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

like image 1000
G.S Bhangal Avatar asked Nov 16 '11 13:11

G.S Bhangal


People also ask

What is DataContract attribute?

[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.

What is data member in WCF?

Data Member are the fields or properties of your Data Contract class. You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute.

What is data member and data contract in C#?

DataContact. A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

Why do we use DataContract in C#?

It is used to get or set the name for DataContract attribute of this type. It is used to get or set the order of serialization and deserialization of a member. It instructs the serialization engine that member must be present while reading or deserializing. It gets or sets the DataMember name.


1 Answers

What you need to do:

  • You don't need to add [KnownType(typeof(CustomBranches))] in your CustomBranches class. A class always knows about itself.
  • You need to filter the properties to only get those with the [DataMember] attribute (nillls' code returned all of them)
  • The data member properties can also be non-public (it works if the serialization is running in full trust)
  • Data members can also be fields (not only properties), so you need to account for them as well.

This is an example of a code which does 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; }
    }
}
like image 166
carlosfigueira Avatar answered Nov 13 '22 06:11

carlosfigueira