Get property type by MemberExpression

前端 未结 1 489
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 01:48

I ask similar question here , assume this type:

 public class Product {

public string Name { get; set; }
public string Title { get; set; }
public string Cat         


        
1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 02:47

    Try casting body.Member to a PropertyInfo

    public class HelperClass
    {
        public static void Property(Expression> expression)
        {
            var body = expression.Body as MemberExpression;
    
            if (body == null)
            {
                throw new ArgumentException("'expression' should be a member expression");
            }
    
            var propertyInfo = (PropertyInfo)body.Member;
    
            var propertyType = propertyInfo.PropertyType;
            var propertyName = propertyInfo.Name;
        }
    }
    

    0 讨论(0)
提交回复
热议问题