Check if any property of class is null

后端 未结 3 945
深忆病人
深忆病人 2021-01-11 20:21

I have following class:-

public class Requirements
    {
        public string EventMessageUId { get; set; }
        public string ProjectId { get; set; }            


        
相关标签:
3条回答
  • 2021-01-11 20:25

    You're checking if the properties themselves are null (which will never be true), not the values of the properties. Use this instead:

    bool isNull = objRequirement.GetType().GetProperties()
                                .All(p => p.GetValue(objRequirement) != null);
    
    0 讨论(0)
  • 2021-01-11 20:27

    I use the below extension of the object which I use for validating objects that I don't want to have all properties null or empty in order to save some calls to the database. I think it fits your case along with some more.

        /// <summary>
        /// Returns true is all the properties of the object are null, empty or "smaller or equal to" zero (for int and double)
        /// </summary>
        /// <param name="obj">Any type of object</param>
        /// <returns></returns>
        public static bool IsObjectEmpty(this object obj)
        {
            if (Object.ReferenceEquals(obj, null))
                return true;
    
            return obj.GetType().GetProperties()
                .All(x => IsNullOrEmpty(x.GetValue(obj)));
        }
    
        /// <summary>
        /// Checks if the property value is null, empty or "smaller or equal to" zero (for numeric types)
        /// </summary>
        /// <param name="value">The property value</param>
        /// <returns></returns>
        private static bool IsNullOrEmpty(object value)
        {
            if (Object.ReferenceEquals(value, null))
                return true;
    
            if (value.GetType().GetTypeInfo().IsClass)
                return value.IsObjectEmpty();
    
            if (value is string || value is char || value is short)
                return string.IsNullOrEmpty((string) value);
    
            if (value is int)
                return ((int) value) <= 0;
    
            if (value is double)
                return ((double) value) <= 0;
    
            if (value is decimal)
                return ((decimal) value) <= 0;
    
            if (value is DateTime)
                return false;
    
            // ........
            // Add all other cases that we may need
            // ........
    
            if (value is object)
                return value.IsObjectEmpty();
    
            var type = value.GetType();
            return type.IsValueType
                && Object.Equals(value, Activator.CreateInstance(type));
        }
    

    The call is obviously obj.IsObjectEmpty()

    0 讨论(0)
  • 2021-01-11 20:41

    This might do the trick for you

    objRequirement.GetType().GetProperties()
    .Where(pi => pi.GetValue(objRequirement) is string)
    .Select(pi => (string) pi.GetValue(objRequirement))
    .Any(value => String.IsNullOrEmpty(value));
    
    0 讨论(0)
提交回复
热议问题