这是我的功能,它将所有项目放置在数据库中。

 public bool getUserProjects(ref List<erpAssets>userProjects)
        {

            string arguments = "{\"sessionId\":\"" + m_LoggedInUser.sessionId + "\"," +
                                 "\"assetType\":\"" + PROJECT_ASSET_TYPE_NAME + "\"" +
                                "}";
            string response = UrlParser(METHOD_GET_ASSETS, MODULE_ADMINISTRATION, arguments); //calling the function urlParse to get the response from that page

            erpAPIResponse basicResponse = JsonConvert.DeserializeObject<erpAPIResponse>(response);

            if (basicResponse.success.Equals("yes"))
            {
                try
                {

                    erpAssets[] Projects = JsonConvert.DeserializeObject<erpAssets[]>(basicResponse.arguments);
                    userProjects.AddRange(Projects);

                }
                catch (Exception e)
                {

                }

            }
            else return false;

            return true; // sending the response back to client
        }


我的erpAsset类如下:

 class erpAssets
    {
        public string assetId { get; set; }
        public string assetSerialNo { get; set; }
        public string serialNo { get; set; }

        public string assetDescription { get; set; }
        public string assetType { get; set; }
        public string parentId { get; set; }
        public string assetIsTrakable { get; set; }

        public bool isTrackable { get; set; }
        public bool isMovable { get; set; }


        public string assetInheritsRegion { get; set; }

        public string inheritsRegion { get; set; }

        public string assetModel { get; set; }

        public string model { get; set; }

        public erpPoint[] assetRegion { get; set; }

    }


我的erpPoint类如下:

class erpPoint
    {

        public double X { get; set; }
        public double Y { get; set; }
    }


现在我面临的问题是,当assetRegion为null时,我得到以下异常

将值“”转换为类型“ erp.erpPoint []”时出错。

可以正常工作的scenario1:
 {
        “ assetId”:“ 43711”,
        “ assetSerialNo”:“ Sector43”,
        “ assetDescription”:“”,
        “ assetVersion”:“”,
        “ assetIsMovable”:“ f”,
        “ assetType”:“项目”,
        “ assetModel”:“项目”,
        “ parentId”:“ 32537”,
        “ assetIsTrackable”:“ f”,
        “ assetInheritsRegion”:“ f”,
        “ assetRegion”:[
            {
                “ X”:-122.69103924537,
                “ Y”:49.105749366835
            },
            {
                “ X”:-122.69103924537,
                “ Y”:49.119046702041
            },
            {
                “ X”:-122.68010753619,
                “ Y”:49.119046702041
            },
            {
                “ X”:-122.68010753619,
                “ Y”:49.105749366835
            }
        ]
    }

这很好。

方案2:

{
        “ assetId”:“ 64374”,
        “ assetSerialNo”:“ FeedLot”,
        “ assetDescription”:“”,
        “ assetVersion”:“”,
        “ assetIsMovable”:“ t”,
        “ assetType”:“项目”,
        “ assetModel”:“项目”,
        “ parentId”:“ 64374”,
        “ assetIsTrackable”:“ t”,
        “ assetInheritsRegion”:“ f”,
        “ assetRegion”:“”
    }

这是我越来越例外的地方。 assetregion为null,现在我在设置其值时遇到异常

最佳答案

您有两种选择:

使用

{ get; set; }


并且您无法指定主体。

或者,您必须同时为getter和setter声明一个主体。

另请注意,double不可为空。请改用double?

private double? x;

public double? X
{
    get { return this.x; }

    set
    {
        if (value != null)
        {
            this.x = value;
        }
    }
}

关于c# - 如何设置变量的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20839718/

10-12 17:19