如何实现只读属性 [英] how to implement a read only property

查看:126
本文介绍了如何实现只读属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,这已经被问。虽然我还没有发现这个计算器上的讨论。请重定向我,如果有重复的这一个。

i am pretty sure this has been asked. although i have not found a discussion about this on stackoverflow. please redirect me if there is a duplicate to this one.

我需要实现我喜欢的类型只读属性。而且该属性的值会在构造函数中进行设置,它是不会被改变(我写一个公开的自定义路由为WPF UI命令,但它并不重要一类)。

i need to implement a read only property on my type. moreover the value of this property is going to be set in the constructor and it is not going to be changed (i am writing a class that exposes custom routed UI commands for WPF but it does not matter).

我看到两种方法可以做到这一点:

i see two ways to do it:

1


class MyClass
{
    public readonly object MyProperty = new object();
}



2

2.


class MyClass
{
    private readonly object my_property = new object();
    public object MyProperty { get { return my_property; } }
}



所有这些FxCop的错误,说我不应该有公共的成员变量,似乎第二个是做正确的方式。是否正确?

with all these FxCop errors saying that i should not have public member variables, it seems that the second one is the right way to do it. correct?

有没有在这种情况下只获得财产和只读成员?

is there any difference between a get only property and a read only member in this case?

有什么区别我沃尔德欣赏任何意见/建议的/ etc。

i wold appreciate any comments/advice/etc.

康斯坦丁

推荐答案

版本:

我认为这并没有太大的区别,如果你只对源代码的兼容性有兴趣的结果
使用一个属性是二进制的更好。 。
:既然兼容性您可以通过它有没有打破取决于你的库的编译代码的setter属性替换

Versioning:
I think it doesn't make much difference if you are only interested in source compatibility.
Using a property is better for binary compatibility since you can replace it by a property which has a setter without breaking compiled code depending on your library.

会议
你所遵循的惯例。在这样的情况下,这两种可能性之间的差异相对较小下面的约定是更好的。一种情况可能回来咬你的是基于反射的代码。它可能只接受性质,而不是领域,例如一个属性编辑器/查看器。

Convention:
You are following the convention. In cases like this where the differences between the two possibilities are relatively minor following the convention is better. One case where it might come back to bite you is reflection based code. It might only accept properties and not fields, for example a property editor/viewer.

连载

从更改现场财产可能会打破了很多序列化。而据我所知的XmlSerializer 并只序列化公共属性,而不是公共领域。

Serialization
Changing from field to property will probably break a lot of serializers. And AFAIK XmlSerializer does only serialize public properties and not public fields.

使用Autoproperty

另一种常见的变化是使用了私人二传手的autoproperty。虽然这是短期和属性不会强制执行readonlyness。所以我更喜欢其他的人

Using an Autoproperty
Another common Variation is using an autoproperty with a private setter. While this is short and a property it doesn't enforce the readonlyness. So I prefer the other ones.

只读域selfdocumenting

目前,虽然该领域的一个优点:

这使得它在在公共接口一目了然,它实际上是不可变的(除非反射)明确。而在财产的情况下,你只能看到的的不能改变它,所以你必须要参考的文档或执行。

Readonly field is selfdocumenting
There is one advantage of the field though:
It makes it clear at a glance at the public interface that it's actually immutable (barring reflection). Whereas in case of a property you can only see that you cannot change it, so you'd have to refer to the documentation or implementation.

不过说实话我使用第一个经常在应用程序的代码,因为我很懒。在图书馆,我通常更彻底,并按照约定。

But to be honest I use the first one quite often in application code since I'm lazy. In libraries I'm typically more thorough and follow the convention.

C#6.0增加了只读自动属性

public object MyProperty { get; }



所以,当你不需要支持旧的编译器你可以有代码一个真正的只读属性这只是作为一个只读域精简。

So when you don't need to support older compilers you can have a truly readonly property with code that's just as concise as a readonly field.

这篇关于如何实现只读属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆