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
[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.
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.
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.
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.
What you need to do:
[KnownType(typeof(CustomBranches))]
in your CustomBranches
class. A class always knows about itself.[DataMember]
attribute (nillls' code returned all of them)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; }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With