如何检查类的所有属性是null还是空?

时间:2015-08-01 06:44:41

标签: c#

我需要一个方法来检查所有类属性的null值,如果只有一个属性为null,则返回false。像这样的东西:

public static bool Check<T>(T instance)
{
    foreach (var parameter in instance)
    {
        if (parameter == null)
            return false;
    }

    return true;
}

我该怎么做?

更新 : 这不是一个重复的问题!我已经看到了这个link,不同的是我的类属性有不同的类型(stringintdouble ....)。所以我需要一种方法来检查它们。这是我的一个班级(其中一些只有字段。)

public class WS_IN_SimpayTransaction
{
    [DataMember]
    public WS_IN_WebServiceIdentity wsIdentity;
    [DataMember]
    public WS_IN_SimpayTransactionParams simpayTransactionParams;
}
public class WS_IN_WebServiceIdentity
{
    private string WS_userName;
    private string WS_passWord;

    public string WS_UserName
    {
        set { this.WS_userName = value; }
        get { return this.WS_userName; }
    }
    public string WS_PassWord
    {
        set { this.WS_passWord = value; }
        get { return this.WS_passWord; }
    }
}
public class WS_IN_SimpayTransactionParams
{
        string amount;
        string itemDes;
        string productID;
        string mobileNumber;
        string bankType;
        double bankTransactionID;
        int transID;
        DateTime date;
        public string Amount
        {
            set { amount = value; }
            get { return amount; }
        }
        public string ItemDes
        {
            set { itemDes = value; }
            get { return itemDes; }
        }
        public string ProductID
        {
            set { productID = value; }
            get { return productID; }
        }
        public string MobileNumber
        {
            set { mobileNumber = value; }
            get { return mobileNumber; }
        }
        public string BankType
        {
            set { bankType = value; }
            get { return bankType; }
        }
        public double BankTransactionID
        {
            set { bankTransactionID = value; }
            get { return bankTransactionID; }
        }
        public int TransID
        {
            set { transID = value; }
            get { return transID; }
        }
        public DateTime Date
        {
            set { date = value; }
            get { return date; }
        }
}

长话短说。它有点复杂。

2 个答案:

答案 0 :(得分:2)

你可以找到你的答案here,它可能会帮助你

指出:

printf()

答案 1 :(得分:2)

像这样(检查null,注意,非字符串属性可以为空):

// You don't need generic, Object is quite enough 
public static bool Check(Object instance) {
  // Or false, or throw an exception
  if (Object.ReferenceEquals(null, instance))
    return true;

  //TODO: elaborate - do you need public as well as non public properties? Static ones?
  var properties = instance.GetType().GetProperties(
    BindingFlags.Instance | 
    BindingFlags.Static | 
    BindingFlags.Public | 
    BindingFlags.NonPublic);

  foreach (var prop in properties) {
    if (!prop.CanRead) // <- exotic write-only properties
      continue;
    else if (prop.PropertyType.IsValueType) // value type can't be null
      continue;

    Object value = prop.GetValue(prop.GetGetMethod().IsStatic ? null : instance);

    if (Object.ReferenceEquals(null, value))
      return false;

    //TODO: if you don't need check STRING properties for being empty, comment out this fragment
    String str = value as String; 

    if (null != str)
      if (str.Equals(""))
        return false;
  }

  return true;
}

编辑:提供的更新示例显示,您要检查字段以及属性,而不是属性;在那种情况下:

// You don't need generic, Object is quite enough 
public static bool Check(Object instance) {
  // Or false, or throw an exception
  if (Object.ReferenceEquals(null, instance))
    return true;

  //TODO: elaborate - do you need public as well as non public field/properties? Static ones? 
  BindingFlags binding =
    BindingFlags.Instance |
    BindingFlags.Static |
    BindingFlags.Public |
    BindingFlags.NonPublic;

  // Fields are easier to check, let them be first
  var fields = instance.GetType().GetFields(binding);

  foreach (var field in fields) {
    if (field.FieldType.IsValueType) // value type can't be null
      continue;

    Object value = field.GetValue(field.IsStatic ? null : instance);

    if (Object.ReferenceEquals(null, value))
      return false;

    //TODO: if you don't need check STRING fields for being empty, comment out this fragment
    String str = value as String;

    if (null != str)
      if (str.Equals(""))
        return false;

    // Extra condition: if field is of "WS_IN_" type, test deep:
    if (field.FieldType.Name.StartsWith("WS_IN_", StringComparison.OrdinalIgnoreCase))  
      if (!Check(value))
        return false; 
  }

  // No null fields are found, let's see the properties
  var properties = instance.GetType().GetProperties(binding);

  foreach (var prop in properties) {
    if (!prop.CanRead) // <- exotic write-only properties
      continue;
    else if (prop.PropertyType.IsValueType) // value type can't be null
      continue;

    Object value = prop.GetValue(prop.GetGetMethod().IsStatic ? null : instance);

    if (Object.ReferenceEquals(null, value))
      return false;

    //TODO: if you don't need check STRING properties for being empty, comment out this fragment
    String str = value as String;

    if (null != str)
      if (str.Equals(""))
        return false;
  }

  return true;
}
相关问题