【问题标题】:Reflection (?) - Check for null or empty for each property/field in a class?反射 (?) - 检查类中每个属性/字段的 null 或空?
【发布时间】:2018-06-29 04:33:30
【问题描述】:

我有一个简单的类:

public class FilterParams
{
    public string MeetingId { get; set; }
    public int? ClientId { get; set; }
    public string CustNum { get; set; }
    public int AttendedAsFavor { get; set; }
    public int Rating { get; set; }
    public string Comments { get; set; }
    public int Delete { get; set; }
}

如何检查类中的每个属性,如果它们不是 null (int) 或 empty/null (for string),那么我将转换该属性的值并将其添加到 List<string>

谢谢。

【问题讨论】:

    标签: c# .net reflection


    【解决方案1】:

    您可以使用 LINQ 来做到这一点:

    List<string> values
        = typeof(FilterParams).GetProperties()
                              .Select(prop => prop.GetValue(yourObject, null))
                              .Where(val => val != null)
                              .Select(val => val.ToString())
                              .Where(str => str.Length > 0)
                              .ToList();
    

    【讨论】:

    • 如果属性的类型为 int 和值 0prop.GetValue 是否返回 null
    • @dtb,不,在这种情况下它会返回0
    • @Frederic:您是要包含 0 还是要过滤掉它们?
    • @James,我想把它们包括在内,正如提问者显然所做的那样。
    • 抱歉,这有点老了,但是如果我想检查一个 short、int、long 是否等于 0 怎么办?查询会是什么样子...我编写了一些代码,但坦率地说,它看起来很丑。
    【解决方案2】:

    不是最好的方法,但大致:

    假设obj 是您的类的实例:

    Type type = typeof(FilterParams);
    
    
    foreach(PropertyInfo pi in type.GetProperties())
    {
      object value = pi.GetValue(obj, null);
    
      if(value != null && !string.IsNullOrEmpty(value.ToString()))
         // do something
    }
    

    【讨论】:

    • 我认为是值对象空检查。需要检查 And (&&) 条件而不是 OR (||)
    【解决方案3】:

    如果你没有很多这样的类并且没有太多的属性,最简单的解决方案可能是写一个iterator block来检查和转换每个属性:

    public class FilterParams
    {
        // ...
    
        public IEnumerable<string> GetValues()
        {
            if (MeetingId != null) yield return MeetingId;
            if (ClientId.HasValue) yield return ClientId.Value.ToString();
            // ...
            if (Rating != 0)       yield return Rating.ToString();
            // ...
        }
    }
    

    用法:

    FilterParams filterParams = ...
    
    List<string> values = filterParams.GetValues().ToList();
    

    【讨论】:

      【解决方案4】:
      PropertyInfo[] properties = typeof(FilterParams).GetProperties();
      foreach(PropertyInfo property in properties)
      {
          object value = property.GetValue(SomeFilterParamsInstance, null);
          // preform checks on value and etc. here..
      }
      

      【讨论】:

        【解决方案5】:

        这是一个例子:

        foreach (PropertyInfo item in typeof(FilterParams).GetProperties()) {
            if (item != null && !String.IsNullOrEmpty(item.ToString()) {
                //add to list, etc
             } 
        }
        

        【讨论】:

          【解决方案6】:

          你真的需要反思吗?实现类似的属性 bool IsNull 是否适合您?你可以将它封装在像 INullableEntity 这样的接口中,并在每个需要这种功能的类中实现,显然如果有很多类,你可能不得不坚持反射。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-09-17
            • 1970-01-01
            • 2013-05-12
            • 1970-01-01
            • 2013-04-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多