gpt4 book ai didi

c# - 无法使用集合初始值设定项初始化类型

转载 作者:行者123 更新时间:2023-11-30 19:25:23 24 4
gpt4 key购买 nike

我有一个类选项。

public class Option
{
public bool Aggregation { get; set; }
public PropertyOptions Property { get; set; }
public bool DoEvent { get; set; }
}

PropertyOptions 是这样的..

public enum PropertyOptions
{
[EnumMember]
On = 0,
[EnumMember]
Off = 1,
[EnumMember]
Auto = 2,
}

现在我有一个返回类 Option 对象的方法

Option setOptions()
{
return new Option()
{
Aggregation = true,
Property = new PropertyOptions()
{
PropertyOptions.Auto,
},
DoEvent = true,
};
}

这里我收到一条错误消息“无法使用集合初始化器初始化类型 PropertyOptions,因为它没有实现 System.Collection.IEnumerable”

我不确定如何设置数据成员“Property”。如果有人能让我注意到可能出现的错误以及我该如何纠正它,那将会很有帮助?

最佳答案

您需要使用常规赋值。

new Option()
{
Aggregation = true,
Property = PropertyOptions.Auto,
DoEvent = true
}

您尝试使用的语法用于集合初始化。例如:

var list = new List<string>
{
"apple",
"banana"
};

您的 Property 属性不是集合。

关于c# - 无法使用集合初始值设定项初始化类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28952658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com