简体   繁体   English

检查对象的任何属性是否为null或为空的最佳方法是什么?

[英]What is best way to check if any of the property of object is null or empty?

I am creating Json file, and before creating i want to check if any of the property is empty of not.. And I want to create abstract method for that. 我正在创建Json文件,并且在创建之前我想检查是否任何属性都为空。.并且我想为此创建抽象方法。 So i dont have to write again and again. 所以我不必一次又一次地写。

public JObject CreatUserJson(Account lacc)
        {
            JObject pin = new JObject(
                new JProperty("email", lacc.email),
                new JProperty("fullname", lacc.fullname),
                new JProperty("phonenumber", lacc.phonenumber),
                new JProperty("ip_address", lacc.ip_address),
                new JProperty("password", lacc.password),
                new JProperty("client_id", Settings.Globals.CLIENT_ID),
                new JProperty("client_secret", Settings.Globals.CLIENT_SECRET)

        );
            return pin;
        }

This is how i defined my method and there are similar methods like this, I want standard way to check and throw exception if any value is missing.. 这就是我定义我的方法的方式,并且有类似的方法,我想用标准的方法来检查并抛出缺失的异常。

public JObject IncomingWireNoticeJson(SyanpasePayLib.Resources.Wire lWire)
        {
            JObject pin = new JObject(
                new JProperty("amount", lWire.amount),
                new JProperty("status_url", lWire.status_url),
                new JProperty("memo", lWire.memo),
                new JProperty("oauth_consumer_key", lWire.oauth_consumer_key)
                );
            return pin;
        }

This is other example of method,there is no similarity. 这是方法的另一个例子,没有相似之处。 I just want to loop through and throw exception if any of the values are missing. 我只想遍历并在缺少任何值的情况下引发异常。

For instance, I know for CreatUserJson I require minimun 4 inputs and max 8 inputs.. 例如,我知道CreatUserJson需要CreatUserJson 4个输入和最多8个输入。

Same way for IncomingWireNoticeJson I require minimun 2 inputs and max 4 inputs.. 对于IncomingWireNoticeJson方式相同,我需要最少2个输入和最多4个输入。

If range is greater then or less then the min and max, then it should throw error.. (This part i can manage, but i dont know how to define standard way to loop through this object) 如果范围大于或小于最小值和最大值,那么它将引发错误。(我可以管理这一部分,但我不知道如何定义循环遍历此对象的标准方式)
Can some one help me with this.. ? 有人可以帮我弄这个吗.. ?

I think JObject has a method called Properties(). 我认为JObject有一个称为Properties()的方法。 So you can loop through the results of that and check whether the values are not null. 因此,您可以遍历结果,并检查值是否不为null。

foreach (JProperty property in pin.Properties()) 
{
    if (string.IsNullOrWhiteSpace(property.Value)) 
    {
        throw new Exception("Some exception");
        //Or perform count for minimum/maximum check 
    }
}

The minimum and maximum check is also easy todo if you use the Properties() method. 如果使用Properties()方法,则最小和最大检查也很容易。 Example can be easily rewritten using Linq too, but for explanation and expanding logic purposes I have written the normal version. 也可以使用Linq轻松重写示例,但是出于解释和扩展逻辑目的,我编写了普通版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM