英文:
Provide value to property on construction time, without doing it inline
问题
I would like to do a refactoring that will let me define the collection of fields somewhere else than inline at the initialization of Fields
.
I'm confused by the error message as I'm still providing the value when it's constructed, I'm not modifying A
after it's constructed (as far as I know).
Can someone please help me understand what I'm doing wrong and tell me how to provide a list of Field
s that are not defined inline?
Definition of A:
public record A
{
public A(String text)
{
this.Fields = new List<Field>();
}
public List<Field> Fields { get; }
}
Example before:
public static class MyClass
{
public static A Build()
{
return new A("some date")
{
Fields =
{
Field1,
Field2
}
}
}
}
Example after:
public static class MyClassyClass
{
public static List<Field> GetMeTheFields()
{
return new List<Field>
{
Field1,
Field2
};
}
public static A Build()
{
return new A("some date")
{
Fields = GetMeTheFields()
}
}
}
But I get the following error message:
A property without setter or inaccessible setter cannot be assigned to
英文:
I would like to do a refactoring that will let me define the collection of fields somewhere else than inline at the initialization of Fields
.
I'm confused by the error message as I'm still providing the value when it's constructed, I'm not modifying A
after it's constructed (as far as I know).
Can someone please help me understand what I'm doing wrong and tell me how to provide a list of Field
s that are not defined inline?
Definition of A:
public record A
{
public A(String text)
{
this.Fields = new List<Field>();
}
public List<Field> Fields { get; }
}
Example before:
public static class MyClass
{
public static A Build()
{
return new A("some date")
{
Fields =
{
Field1,
Field2
}
}
}
}
Example after:
public static class MyClassyClass
{
public static List<Field> GetMeTheFields()
{
return new List<Field>
{
Field1,
Field2
};
}
public static A Build()
{
return new A("some date")
{
Fields = GetMeTheFields()
}
}
}
But I get the following error message:
> A property without setter or inaccessible setter cannot be assigned to
答案1
得分: 2
Add an init
clause to the Fields
property definition and you are good to go.
public List<Field> Fields { get; init; }
This will allow you to modify the Fields
property inside the constructors or via an object initializer.
Your code has failed because the property without set
or init
can't be initialized inside the object initializer.
英文:
Add an init
clause to the Fields
property definition and you are good to go.
public List<Field> Fields { get; init; }
This will allow you to modify the Fields
property inside the constructors or via an object initializer.
Your code has failed because the property without set
or init
can't be initialized inside the object initializer.
答案2
得分: 1
如果您无法修改A
类以在Fields
属性上添加一个init
setter,那么根据您的“后续示例”,以下是另一种方法,如何以不同的方式将Field
列表提供给Fields
:
public static class MyClassyClass
{
public static List<Field> GetMeTheFields()
{
return new List<Field>
{
Field1,
Field2
};
}
public static A Build()
{
var a = new A("some date");
a.Fields.AddRange(GetMeTheFields());
return a;
}
}
英文:
In case you cannot modify class A
to add an init
setter to Fields
property, here's how you can provide a list of Field
s to Fields
a different way, based on your "example after":
public static class MyClassyClass
{
public static List<Field> GetMeTheFields()
{
return new List<Field>
{
Field1,
Field2
};
}
public static A Build()
{
var a = new A("some date");
a.Fields.AddRange(GetMeTheFields());
return a;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论