Check if any property of class is null

后端 未结 3 979
深忆病人
深忆病人 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);
    

提交回复
热议问题