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

后端 未结 9 1214
星月不相逢
星月不相逢 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));
    }
    
    0 讨论(0)
  • 2020-11-28 06:57

    The following code returns if any property is not null.

      return myObject.GetType()
                     .GetProperties() //get all properties on object
                     .Select(pi => pi.GetValue(myObject)) //get value for the propery
                     .Any(value => value != null); // Check if one of the values is not null, if so it returns true.
    
    0 讨论(0)
  • 2020-11-28 06:57

    you can use reflection and extension methods to do this.

    using System.Reflection;
    public static class ExtensionMethods
    {
        public static bool StringPropertiesEmpty(this object value)
        {
            foreach (PropertyInfo objProp in value.GetType().GetProperties())
            {
                if (objProp.CanRead)
                {
                    object val = objProp.GetValue(value, null);
                    if (val.GetType() == typeof(string))
                    {
                        if (val == "" || val == null)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
    

    then use it on any object with string properties

    test obj = new test();
    if (obj.StringPropertiesEmpty() == true)
    {
        // some of these string properties are empty or null
    }
    
    0 讨论(0)
  • 2020-11-28 07:06

    Here you go

    var instOfA = new ObjectA();
    bool isAnyPropEmpty = instOfA.GetType().GetProperties()
         .Where(p => p.GetValue(instOfA) is string) // selecting only string props
         .Any(p => string.IsNullOrWhiteSpace((p.GetValue(instOfA) as string)));
    

    and here's the class

    class ObjectA
    {
        public string A { get; set; }
        public string B { get; set; }
    }
    
    0 讨论(0)
  • 2020-11-28 07:07

    No, I don't think there is a method to do exactly that.

    You'd be best writing a simple method that takes your object and returns true or false.

    Alternatively, if the properties are all the same, and you just want to parse through them and find a single null or empty, perhaps some sort of collection of strings would work for you?

    0 讨论(0)
  • 2020-11-28 07:08

    To only check if all properties are null:

    bool allPropertiesNull = !myObject.GetType().GetProperties().Any(prop => prop == null);
    
    0 讨论(0)
提交回复
热议问题