简体   繁体   English

如何使用通用getter和setter设置只读属性的值?

[英]How to set the value of a read-only property with generic getters and setters?

Not sure if I worded this correctly ... but I have the following code: 不确定我是否正确说明了这一点......但我有以下代码:

    public Guid ItemId
    {
        get;
    }

    public TransactionItem()
    {
        this.ItemId = Guid.Empty;
    }

Naturally I am getting a read-only issue ... which I do understand. 当然我得到一个只读的问题......我明白了。 Is there anyway to set this property value without having to do something like the below: 无论如何设置此属性值而不必执行以下操作:

    Guid _itemId = Guid.Empty;
    public Guid ItemId
    {
        get
        {
            return _itemId;
        }
        set
        {
            _itemId = value;
        }
    }

or 要么

    public Guid ItemId
    {
        get;
        internal set;
    }

Thanks in advance! 提前致谢!

I would go for this: 我会这样做:

public Guid ItemId
{
    get;
    private set; // can omit as of C# 6 
}

public TransactionItem()
{
    this.ItemId = Guid.Empty;
}

Of course, it would be open for setting within this class, but since you are writing it I hope you have the sense to not break your own intentions... 当然,在本课程中设置它是开放的,但是既然你正在编写它,我希望你有理由不打破你自己的意图......

In my opinion, things like readonly properties, are mostly important when seen from the outside. 在我看来,从外面看,像readonly属性这样的东西最重要。 From the inside, it doesn't really matter what it is, cause there, you are the King =) 从内到外,它并不重要,因为那里,你是国王=)

If you just need the ItemId property to be read-only for external users of your class, then Svish's answer is the way to go. 如果您只需要ItemId属性对您的类的外部用户是只读的,那么Svish的答案是要走的路。

If you need ItemId to be read-only within the class itself then you'll need to do something like this: 如果您需要ItemId在类本身内是只读的,那么您需要执行以下操作:

private readonly Guid _ItemId
public Guid ItemId
{
    get { return _ItemId; }
}

public TransactionItem()
{
    _ItemId = Guid.Empty;
}

You can use readonly keyword 您可以使用readonly关键字

public readonly Guid ItemId;

public TransactionItem()
{
    this.ItemId = Guid.Empty;
}

I'm afraid your question isn't very clear - but if there isn't a property setter defined, then you certainly can't call it. 我担心你的问题不是很明确 - 但是如果没有定义属性设置器,那么你当然不能称之为。

Are you actually after an automatically implemented read-only property, allowing setting just within the constructor? 实际上是在一个自动实现的只读属性之后,允许在构造函数中进行设置吗? If so, I'm afraid that's not available, much as I'd like it. 如果是这样,我恐怕没有,就像我喜欢的那样。

Just to expand on what I mean, I'd like to be able to do: 只是到了我的意思扩大,我希望能够做到:

// Not valid in C# - yet!
public class Foo
{
    // Autogenerated field would be readonly in IL.
    public string Name { get; readonly set; }

    public Foo (string name)
    {
        this.Name = name;
    }

    public void Bar()
    {
        // This would be invalid
        this.Name = "No!";
    }
}

Basically it would be "make a property like a readonly field." 基本上它将是“使一个属性像一个只读字段。”

Use a private setter, or set the backing field ? 使用私有的setter,还是设置支持字段?
(But, then you must make sure that the name of your backing field can be determined based on the property-name. (但是,您必须确保可以根据属性名称确定支持字段的名称。
For instance by making sure that your backing field always has the same name as the property-name, but is prefixed with an underscore; 例如,确保您的支持字段始终与property-name具有相同的名称,但前缀为下划线; like NHibernate does it using its access-strategies). 就像NHibernate使用其访问策略一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM