Question

I have a datacontract and in that i want to get the name of a property as mentioned in the code below.

 [DataMember]
   public string PhoneNumber { get; set; }

Now i am passing the values as

    void GetMethod()
{

// takes PropertyName and Property values

ValidateMe("PhoneNumber", phoneObj.PhoneNumber.ToString();

}

Now i need to pass instead get the Property Name instead of Hardcoded "PhoneNumber"

How can i achieve this?

Was it helpful?

Solution

string GetPropertyName<TValue>(Expression<Func<TValue>> propertyId)
{
   return ((MemberExpression)propertyId.Body).Member.Name;
}

By this way we can retrieve the Property Name

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top