使用自动实现的属性或采用自行执行的财产 [英] Use automatically implemented property or implement the property by ourself

查看:104
本文介绍了使用自动实现的属性或采用自行执行的财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我们可以创建一个自动实现的属性一样,

In C# we can create a automatically implemented property like that

public string MyValue{get;set;}

不过,我们也可以通过我们自己一样,

However, we also can create a property by ourself like that

private string _myValue;
public string MyValue
{
    get { retrun _myValue; }
    set { _myValue = value; }
}



我的问题是在这种情况下,我们应该使用自动之一,属于哪种情况,我们?应该实现我们自己

My question is in which situation we should use auto one and which situation we should implement ourself?

推荐答案

有是你不应该使用自动实现的属性至少有一个明显的例子:任何形式的两个不同的DLL之间的二进制序列化。

There is at least one clear case in which you should NOT use auto-implemented properties: any sort of binary serialization between two different DLLs.

二进制序列依赖于私有成员和私营后盾成员自动实现的属性都不能保证每个编译的DLL一样,所以二进制序列化可能失败相当厉害。

Binary serialization relies on private members and the private backing members for auto-implemented properties are not guaranteed to be the same in each compiled DLL, so binary serialization could fail quite badly.

另一个原因是没有不使用自动实现的属性是,你将失去控制权的指定支持字段作为非序列化的能力,但它是很容易的创建在这种情况下属性支持字段。

Another reason for not not using auto-implemented properties is that you will lose control over is the ability to specify the backing field as NonSerialized, but it is easy enough to create a backing field for the property in this case.

如果您或您使用任何产品执行的成员反射(即WCF) ,然后你会看到,而不是您创建一个漂亮的支持字段错位的支持字段名。

If you or any product that you use performs reflection on the members (i.e. WCF), then you will see the mangled backing field name instead of a "pretty" backing field that you created.

如果您以前提供给访问这可能是非常重要的服务或者如果你反序列化在接收端到同一类结构(即同一类的WCF管的两端)使用。在这种情况下,你也不一定能反序列化,因为你可以保证,除非你分享,而不是源代码中的同一个DLL支持字段名称是一样的。

This could be very important if you had previously provided access to the service or if you deserialize on the receiving end into the same class structure (i.e. the same classes are used on both ends of the WCF pipe). In this case, you would not necessarily be able to deserialize because you could guarantee that the backing field name is the same unless you share the same DLL as opposed to the source code.

举例来说,假设你有一个公开一些过度WCF的业务对象为您创建一个Silverlight客户端的服务。为了重用你的业务逻辑,你的Silverlight客户端添加到源代码引用你的业务对象。如果您有自动实现的属性,您对支持字段名没有控制权。由于WCF序列化的成员,没有属性,你不能确保对象转移从WCF服务Silverlight将正确反序列化,因为后备字段名称将几乎肯定是不匹配的。

For example, assume that you have a service that exposes some of your business objects over WCF to a silverlight client that you have created. In order to reuse your business logic, your Silverlight client adds references to the source code for your business objects. If you have auto-implemented properties, you have no control over the backing field name. Since WCF serializes the members and not the properties, you cannot be sure that the object transferred to silverlight from the WCF service will deserialize correctly because the backing field names will almost certainly be mismatched.

这篇关于使用自动实现的属性或采用自行执行的财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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