用作属性的 C# 泛型对象列表 - 无法添加值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/833442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:27:36  来源:igfitidea点击:

C# generics list of objects used as a property - can't add values

c#genericsproperties

提问by Nils

I'm trying generics for the first time and am having a problem.

我第一次尝试泛型并且遇到了问题。

I have a dll that sends messages in batches

我有一个可以批量发送消息的dll

  • there is a "Message" class and a "Batch" class in that dll

  • on the batch class, I have some public properties

  • on of the batch class's public properties is a property called "Messages" which is a list of the "Message" class as follows:

     public List<Message> Messages {get;set;}
    
  • 该dll中有一个“Message”类和一个“Batch”类

  • 在批处理类上,我有一些公共属性

  • 批处理类的公共属性中有一个名为“Messages”的属性,它是“Message”类的列表,如下所示:

     public List<Message> Messages {get;set;}
    

Method 1

方法一

I then have a test exe where I want to set the properties on the "Batch" class as follows:

然后我有一个测试 exe,我想在其中设置“批处理”类的属性,如下所示:

Batch myBatch = new Batch()
myBatch.Messages.Add(
  new MyNameSpace.Message(txtToAddress.Text, txtMessage.Text));

When I run the app, I get:

当我运行应用程序时,我得到:

"Object reference not set to an instance of an object."

“你调用的对象是空的。”

Method 2

方法二

After playing around a bit, I see that I can successfully do the following in the test exe:

玩了一会儿后,我发现我可以在测试 exe 中成功执行以下操作:

List<MyNameSpace.Message> myMessages = new List<MyNameSpace.Message>();
myBatch.Messages.Add(
 new MyNameSpace.Message(txtToAddress.Text, txtMessage.Text));
myBatch.Messages = myMessages;

I'd like to get it working in the first way because other programmers will be using the dll and it seems more intutive to use the first approach.

我想让它以第一种方式工作,因为其他程序员将使用 dll,使用第一种方法似乎更直观。

What am I missing to get the first method to work?

让第一种方法起作用我缺少什么?

采纳答案by Marc Gravell

Normally, collections are initialized by the parent object:

通常,集合由父对象初始化:

public List<Message> Messages {get; private set;}

public Batch() { // constructor
    Messages = new List<Message>();
}

Now it should work as expected. Note that if you are using XmlSerializeryou'll need to keep the public set too...

现在它应该按预期工作。请注意,如果您正在使用,则还XmlSerializer需要保留公共集...

In some ways, the long-hand property code is easier here:

在某些方面,这里的长手属性代码更容易:

private List<Message> messages = new List<Message>();
public List<Message> Messages { get {return messages; } }

(no messing with constructors, etc)

(不要弄乱构造函数等)

回答by Eoin Campbell

You need to instantiate your list first.

您需要先实例化您的列表。

Add this to your contstructor

将此添加到您的构造函数

Messages = new List<Message>();

回答by RvdK

Batch myBatch = new Batch()
myBatch.Messages.Add(

After creating a new batchthe Messages List is probably not created yet. Create the List in the constructor of Batch.

创建新批次后,可能尚未创建消息列表。在 Batch 的构造函数中创建 List。

回答by Guffa

In the constructor of the Batchclass, create a list for the Messagesproperty:

Batch类的构造函数中,为Messages属性创建一个列表:

public Batch() {
   Messages = new List<Messages>();
}

回答by Tim Jarvis

Your Batch class would have to be responsible for creating an instance of the List probably in the constructor would be the best place.

您的 Batch 类必须负责创建 List 的实例,可能在构造函数中是最好的地方。