在构建时为属性提供值,而不是内联方式。

huangapple go评论122阅读模式
英文:

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 Fields 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 Fields that are not defined inline?


Definition of A:

public record A
{
    public A(String text)
    {
        this.Fields = new List&lt;Field&gt;();
    }

    public List&lt;Field&gt; Fields { get; }
}

Example before:

public static class MyClass
{
    public static A Build()
    {
        return new A(&quot;some date&quot;)
        {
            Fields =
            {
                Field1,
                Field2
            }
        }
    }
}

Example after:

public static class MyClassyClass
{
    public static List&lt;Field&gt; GetMeTheFields()
    {
        return new List&lt;Field&gt;
        {
            Field1,
            Field2
        };
    }

    public static A Build()
    {
        return new A(&quot;some date&quot;)
        {
            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&lt;Field&gt; 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 Fields to Fields a different way, based on your "example after":

public static class MyClassyClass
{
    public static List&lt;Field&gt; GetMeTheFields()
    {
        return new List&lt;Field&gt;
        {
            Field1,
            Field2
        };
    }

    public static A Build()
    {
        var a = new A(&quot;some date&quot;);

        a.Fields.AddRange(GetMeTheFields());

        return a;
    }
}

huangapple
  • 本文由 发表于 2023年8月10日 16:46:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定