How to check all properties of an object whether null or empty?

后端 未结 9 1361
星月不相逢
星月不相逢 2020-11-28 06:41

I have an object lets call it ObjectA

and that object has 10 properties and those are all strings.

 var myObject = new {Property1=\"\",P         


        
9条回答
  •  我在风中等你
    2020-11-28 06:54

    A slightly different way of expressing the linq to see if all string properties of an object are non null and non empty:

    public static bool AllStringPropertyValuesAreNonEmpty(object myObject)
    {
        var allStringPropertyValues = 
            from   property in myObject.GetType().GetProperties()
            where  property.PropertyType == typeof(string) && property.CanRead
            select (string) property.GetValue(myObject);
    
        return allStringPropertyValues.All(value => !string.IsNullOrEmpty(value));
    }
    

提交回复
热议问题