【问题标题】:How to access the member properties of class that is of list<class> type如何访问 list<class> 类型的类的成员属性
【发布时间】:2020-04-03 18:00:54
【问题描述】:

目前我有以下 Json,我将其转换为普通对象 C# 类。我的普通对象类将它们放在一个 ModelMock 类中,我试图在其中访问属性“名称”和“值”。但我无法访问,我得到了错误。我得到的错误:“列表”不包含“名称”和“值”的定义

这是 ref 的 Json:

 {
"matchActionsReasons": [
   {
     "name": "False positive",
     "value": -2147483648
   },
   {
     "name": "Acceptable risk",
     "value": -2147483647
   },
   {
     "name": "Manager approval",
     "value": -2147483646
   }
 ]
}

这是我的模型类:

public class ModelMock
    {
        public static ModelMock SaveSettingsModel()
        {
            return new ModelMock
            {

                matchActionsReasons =new List<MatchActionsReason>
                {    

                         name = "False positive",  **//Geting the error here**
                         value  = -2147483648      **//Geting the error here**
                }

            };

        }

        public class MatchActionsReason
        {
            public string name { get; set; }
            public int value { get; set; }
        }


            public List<MatchActionsReason> matchActionsReasons { get; set; }

    }

【问题讨论】:

  • 我添加了一个带有解释和文档链接的答案。我还添加了一个代码示例。

标签: c# json api serialization


【解决方案1】:

您必须创建和添加 MatchActionsReason 类型的对象:

// Using a collection initializer for the list
// and object initializers for the items
matchActionsReasons = new List<MatchActionsReason>{    
    new MatchActionsReason{ name = "False positive", value = -2147483648 }, 
    new MatchActionsReason{ name = "Acceptable risk", value = -2147483647 }, 
    new MatchActionsReason{ name = "Manager approval", value = -2147483646 }
};

// Using Add
matchActionsReasons = new List<MatchActionsReason>();
matchActionsReasons.Add(
    new MatchActionsReason{ name = "False positive", value = -2147483648 }
);
matchActionsReasons.Add(
    new MatchActionsReason{ name = "Acceptable risk", value = -2147483647 }
);  
matchActionsReasons.Add(
    new MatchActionsReason{ name = "Manager approval", value = -2147483646 }
); 

List&lt;T&gt; 类本身没有 namevalue 属性。除其他外,它还有一个 Count 属性。

您可以像这样访问列表元素的成员:

string s = matchActionsReasons[1].name; // ==> "Acceptable risk"

MatchActionsReason mar = matchActionsReasons[1];
string s = mar.name; // ==> "Acceptable risk"

【讨论】:

  • 感谢您帮助设置值。但是我得到“System.NullReferenceException:对象引用未设置为对象的实例”我认为它是因为新的 MatchActionsReason 实例?你知道我该如何解决这个问题?
  • 我只能猜测。当你创建一个数组时,比如说var a = new MatchActionsReason[20];,那么它首先填充了20 个null 条目。此外,当您创建一个类变量时,即所谓的字段,它首先使用null 进行初始化。您必须先使用new 创建和分配对象,然后才能访问它们。
【解决方案2】:

你必须这样写:

matchActionsReasons =new List<MatchActionsReason>
 {    

       new MatchActionReason
       {
           name = "False positive",
           value  = -2147483648
       },
       add more instances here...          
 }

所以完整的示例如下所示:

public class ModelMock
{
    public static ModelMock SaveSettingsModel()
    {
        return new ModelMock
        {

            matchActionsReasons =new List<MatchActionsReason>
            {    
                 new MatchActionReason
                 {
                     name = "False positive",
                     value  = -2147483648
                 },
                 new MatchActionReason
                 {
                     name = "Another False One",
                     value  = -111111111
                 },
                 add more instances here...   
            }

        };

    }

    public class MatchActionsReason
    {
        public string name { get; set; }
        public int value { get; set; }
    }


        public List<MatchActionsReason> matchActionsReasons { get; set; }

}

原因是当你做 new List { put here the new instances }

因此,在括号之间,您将添加到列表中的实例。

这称为集合初始化器。

这是 Microsoft 针对 Obejct and Collection Initializers 的文档

【讨论】:

  • 感谢乔纳森提供答案和文档,我能够设置值。但是在创建新实例时,我得到一个 null System.NullReferenceException : Object reference not set to an instance of an object。你知道我怎么能解决这个问题吗?
  • @ABIDKHAN 你在哪里得到异常?我会帮你解决的。
  • @ Jonathan Alfaro 当我调试它时我不确定哪一行给了我空引用,但我相信它在创建新实例“new MatchActionReason”时是这样的
  • @ABIDKHAN 您需要设置断点并单步执行代码以查看它发生的位置。此外,如果您使用我在答案中发布的代码,您应该不会遇到任何异常。最后,您需要发布异常的整个堆栈跟踪。
  • 感谢 Jonathan 我已对其进行了调试并解决了问题。再次感谢您的帮助:)
【解决方案3】:

您需要将一个新的 MatchActionsReason 对象添加到您的列表中。如下所示。

matchActionsReasons = new List<MatchActionsReason>();
matchActionsReasons.Add(new MatchActionsReason(){ name = "foo", value = 115 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2017-09-20
    • 1970-01-01
    相关资源
    最近更新 更多