C# 试图在自定义对象中将集合公开为数组
我目前正在尝试这样做: json.Properties[ssn].Value= 这背后的代码如下所示:C# 试图在自定义对象中将集合公开为数组,c#,C#,我目前正在尝试这样做: json.Properties[ssn].Value= 这背后的代码如下所示: public JsonProperty this[String name] { get { for (int i = 0; i < this.Properties.Count; i++) { if (this.Properties[i].Name.Equals(name)) {
public JsonProperty this[String name]
{
get
{
for (int i = 0; i < this.Properties.Count; i++)
{
if (this.Properties[i].Name.Equals(name))
{
return Properties[i];
}
}
throw new ArgumentNullException("No property with that name exists");
}
set
{
for (int i = 0; i < this.Properties.Count; i++)
{
if (this.Properties[i].Name.Equals(name))
{
this.Properties[i].Value = value;
break;
}
}
}
}
Intellisense在这一行给了我一个错误:this.Properties[i].Value=Value
它告诉我:
无法修改“System.Collections.Generic.List.this[int]”的返回值,因为它不是变量
我不确定该如何解决这个问题。有什么建议吗?我不知道JsonProperty类的体系结构, 但似乎您正在尝试将索引器JsonProperty的RHS表单输入到 属性[i]。值,它是JsonProperty.Value,而不仅仅是JsonProperty 尝试将行更改为:
this.Properties[i] = value;
或者这对于索引器来说是错误的
this.Properties[i].Value = value.Value;
你已经回答了你的问题。不能直接修改结构成员 您需要做的是创建新的JSONProperty并分配给属性列表
Properties[i] = new JsonProperty(name, value);//don't know exact JsonProperty constructor
或
this.Properties[i]=值@如果你只是问我那一行,而不是那样做,CarbineCoder会更容易。你使用JSON的框架是什么?你可能想考虑使用字典而不是列表。JSONNECT只是一个有两个字符串的结构。名称和值。我希望能够在我的对象上使用[]运算符来查找JsonProperty的值。不过,setter将在等式的RHS上接收JsonProperty,而不是字符串。
JsonProperty temp = Properties[i];
temp.Value = value;
Properties[i] = temp;