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?

有帮助吗?

解决方案

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

By this way we can retrieve the Property Name

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top