它是一个重大更改,修改一个公共属性的访问修饰符? [英] Is it a breaking change that modifying the access modifier of a public property?

查看:118
本文介绍了它是一个重大更改,修改一个公共属性的访问修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我改变私人的公共属性的setter方法​​的访问修饰符为public,是导致中引用它的其他组件的任何重大更改?

If I change the access modifier of the setter of a public property from private to public, is that causes any breaking change in the other assemblies that refer it?

推荐答案

更新This问题是我的博客在2012年1月的话题。感谢伟大的问题!

UPDATE: This question was the topic of my blog in January 2012. Thanks for the great question!

我认为是重大更改你的意思是当我重新编译依赖代码本次大会,这是否用来编译还是编译代码吗?

I assume that by "breaking change" you mean "when I recompile code that depended on this assembly, does code that used to compile still compile?"

根据这一定义,严格来说,是的,使得属性setter公共曾经是私属破更改。假设你有这样的代码:

By that definition, strictly speaking, yes, making a property setter public that used to be private is a breaking change. Suppose you have this code:

// Assembly Alpha
public class Charlie
{
    public int P { get; private set; }
}
public class Delta
{
    public int P { get; set; }
}

然后在引用阿尔法另一个程序集:

And then in another assembly that references Alpha:

// Assembly Echo
class Foxtrot
{
    static void M(Action<Charlie> f) {}
    static void M(Action<Delta> f) {}
    static void Golf()
    {
        M(y=>{y.P = 123;});
    }
}

您编译装配回声。狐步舞类有一个方法高尔夫这确实对M.中号重载有两个重载;一个发生在查理的行动,而另三角洲上一个动作。如果拉姆达参数y的类型是查理则在lambda体内产生的可访问性错误,因此M的是超载不适用的候选人。重载决策选择第二个过载和编译成功。

You compile assembly Echo. Class Foxtrot has a method Golf which does overload resolution on M. M has two overloads; one that takes a action on Charlie and one that takes an action on Delta. If lambda parameter y is of type Charlie then the lambda body produces an accessibility error, and therefore that overload of M is not an applicable candidate. Overload resolution chooses the second overload and compilation succeeds.

现在你改变装配阿尔法让Charlie.P的setter是公共的。重新编译回声。现在,你得到一个重载解析错误,因为M的两个重载都同样有效,也不是优于其他。回声汇编由于阿尔法更改失败。您对阿尔法的变化是一个重大更改。

Now you change assembly Alpha so that Charlie.P's setter is public. You recompile Echo. Now you get an overload resolution error because both overloads of M are equally valid and neither is better than the other. Compilation of Echo fails due to the change in Alpha. Your change to Alpha was a breaking change.

现在的问题不是这是一个重大更改?这显然​​是;几乎每一个变化是某种突破的变化。这个问题应该是重大更改是否会真正在实践中突破的人,如果是这样,是什么相比,新功能的好处固定休息的费用是多少?

The question is not "is this a breaking change?" It clearly is; almost every change is some kind of breaking change. The question should be whether the breaking change will actually in practice break anyone, and if so, what is the cost of fixing the break compared to the benefit of the new feature?

这篇关于它是一个重大更改,修改一个公共属性的访问修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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