문제

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