Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据配置文件验证对象的最简单方法_C#_.net_Json_Automapper - Fatal编程技术网

C# 根据配置文件验证对象的最简单方法

C# 根据配置文件验证对象的最简单方法,c#,.net,json,automapper,C#,.net,Json,Automapper,我正在运行基于nancyfx的restful服务,需要与基于cobol的机器通信 我以json的形式获取请求,并将其绑定到一个对象中。我的configfile也是一个json文件,我在其中设置边界(左或右)、小数点后的数字量以及字段的总长度 我想验证每个字段,稍后需要以给定格式输出每个字段,因为cobol程序需要它的值(例如,-123,45”之类的负值需要在“12345-”模式中) 以下是我在json配置文件中生成的一些属性: public class Companyid {

我正在运行基于nancyfx的restful服务,需要与基于cobol的机器通信

我以json的形式获取请求,并将其绑定到一个对象中。我的configfile也是一个json文件,我在其中设置边界(左或右)、小数点后的数字量以及字段的总长度

我想验证每个字段,稍后需要以给定格式输出每个字段,因为cobol程序需要它的值(例如,-123,45”之类的负值需要在“12345-”模式中)

以下是我在json配置文件中生成的一些属性:

    public class Companyid
    {
        public string bound { get; set; }
        public int decimals { get; set; }
        public int length { get; set; }
    }

    public class Customerid
    {
        public string bound { get; set; }
        public int decimals { get; set; }
        public int length { get; set; }
    }

    public class Zip
    {
        public string bound { get; set; }
        public int decimals { get; set; }
        public int length { get; set; }
    }

    public class RootObject
    {
        public Companyid companyid { get; set; }
        public Customerid customerid { get; set; }
        public Zip zip { get; set; }
    }
json如下所示:

{
    "companyid": {
        "bound": "right",
        "decimals": 0,
        "length": 2,
        "pos": 1
    },
    "customerid": {
        "bound": "right",
        "decimals": 0,
        "length": 6,
        "pos": 2
    },
    "zip": {
        "bound": "right",
        "decimals": 0,
        "length": 5,
        "pos": 13
    }
}
     public class Customer
        {
            public string companyid { get; set; }
            public int customerstatus { get; set; }
            public string customersince { get; set; }
            public string customerid { get; set; }
        }
我的客户对象(用户输入)如下所示:

{
    "companyid": {
        "bound": "right",
        "decimals": 0,
        "length": 2,
        "pos": 1
    },
    "customerid": {
        "bound": "right",
        "decimals": 0,
        "length": 6,
        "pos": 2
    },
    "zip": {
        "bound": "right",
        "decimals": 0,
        "length": 5,
        "pos": 13
    }
}
     public class Customer
        {
            public string companyid { get; set; }
            public int customerstatus { get; set; }
            public string customersince { get; set; }
            public string customerid { get; set; }
        }
最后,我还需要将这些项目按正确的顺序排列(因此将顺序从companyid | customerid | zip更改为zip | companyid | customerid)

为清晰起见,请编辑:

是否有一种简单的方法可以遍历对象的所有属性并接收属性值

我如何改进我的目标,使迭代成为可能/更容易?Automapper是否可以有效解决我的问题?如果是,我将如何实施它