Using PropertyInfo to find out the property type

前端 未结 2 890
無奈伤痛
無奈伤痛 2020-11-30 03:01

I want to dynamically parse an object tree to do some custom validation. The validation is not important as such, but I want to understand the PropertyInfo class better.

2条回答
  •  [愿得一人]
    2020-11-30 03:26

    I just stumbled upon this great post. If you are just checking whether the data is of string type then maybe we can skip the loop and use this struct (in my humble opinion)

    public static bool IsStringType(object data)
        {
            return (data.GetType().GetProperties().Where(x => x.PropertyType == typeof(string)).FirstOrDefault() != null);
        }
    

提交回复
热议问题