我想收集尽可能多的关于。net / clr中API版本控制的信息,特别是API更改如何破坏或不破坏客户端应用程序。首先,让我们定义一些术语:

API change - a change in the publicly visible definition of a type, including any of its public members. This includes changing type and member names, changing base type of a type, adding/removing interfaces from list of implemented interfaces of a type, adding/removing members (including overloads), changing member visibility, renaming method and type parameters, adding default values for method parameters, adding/removing attributes on types and members, and adding/removing generic type parameters on types and members (did I miss anything?). This does not include any changes in member bodies, or any changes to private members (i.e. we do not take into account Reflection).

二进制级中断——一种API更改,导致针对旧版本API编译的客户端程序集可能无法装入新版本。例如:改变方法签名,即使它允许以与以前相同的方式被调用(即:void返回类型/参数默认值重载)。

源代码级中断——API更改会导致针对旧版本API编写的现有代码可能无法使用新版本进行编译。但是,已经编译的客户机程序集与以前一样工作。例如:添加一个新的重载,可能导致之前明确的方法调用出现歧义。

源代码级安静的语义变化——API的变化会导致编写的现有代码针对旧版本的API悄悄地改变其语义,例如通过调用不同的方法。然而,代码应该继续编译,没有警告/错误,以前编译的程序集应该像以前一样工作。示例:在现有类上实现一个新接口,这会导致在重载解析过程中选择不同的重载。

The ultimate goal is to catalogize as many breaking and quiet semantics API changes as possible, and describe exact effect of breakage, and which languages are and are not affected by it. To expand on the latter: while some changes affect all languages universally (e.g. adding a new member to an interface will break implementations of that interface in any language), some require very specific language semantics to enter into play to get a break. This most typically involves method overloading, and, in general, anything having to do with implicit type conversions. There doesn't seem to be any way to define the "least common denominator" here even for CLS-conformant languages (i.e. those conforming at least to rules of "CLS consumer" as defined in CLI spec) - though I'll appreciate if someone corrects me as being wrong here - so this will have to go language by language. Those of most interest are naturally the ones that come with .NET out of the box: C#, VB and F#; but others, such as IronPython, IronRuby, Delphi Prism etc are also relevant. The more of a corner case it is, the more interesting it will be - things like removing members are pretty self-evident, but subtle interactions between e.g. method overloading, optional/default parameters, lambda type inference, and conversion operators can be very surprising at times.

这里有几个例子:

添加新的方法重载

Kind:源级中断

受影响的语言:c#, VB, f#

更改前的API:

public class Foo
{
    public void Bar(IEnumerable x);
}

更改后的API:

public class Foo
{
    public void Bar(IEnumerable x);
    public void Bar(ICloneable x);
}

样例客户端代码在更改前工作,更改后失效:

new Foo().Bar(new int[0]);

添加新的隐式转换运算符重载

Kind:源级中断。

受影响的语言:c#, VB

不受影响语言:f#

更改前的API:

public class Foo
{
    public static implicit operator int ();
}

更改后的API:

public class Foo
{
    public static implicit operator int ();
    public static implicit operator float ();
}

样例客户端代码在更改前工作,更改后失效:

void Bar(int x);
void Bar(float x);
Bar(new Foo());

注意:f#并没有被破坏,因为它对重载操作符没有任何语言级别的支持,既不是显式的也不是隐式的——两者都必须作为op_Explicit和op_Implicit方法直接调用。

添加新的实例方法

Kind:源级安静语义更改。

受影响的语言:c#, VB

不受影响语言:f#

更改前的API:

public class Foo
{
}

更改后的API:

public class Foo
{
    public void Bar();
}

客户端代码示例:

public static class FooExtensions
{
    public void Bar(this Foo foo);
}

new Foo().Bar();

注意:f#并没有被破坏,因为它没有对ExtensionMethodAttribute的语言级支持,并且需要将CLS扩展方法作为静态方法调用。


当前回答

添加具有默认值的参数。

中断类型:二进制级别的中断

即使调用源代码不需要更改,它仍然需要重新编译(就像添加常规参数一样)。

这是因为c#将参数的默认值直接编译到调用程序集中。这意味着如果您不重新编译,您将得到一个MissingMethodException,因为旧程序集试图调用带有较少参数的方法。

更改前的API

public void Foo(int a) { }

更改后的API

public void Foo(int a, string b = null) { }

之后被破坏的示例客户端代码

Foo(5);

客户端代码需要在字节码级别上重新编译为Foo(5, null)。被调用的程序集将只包含Foo(int, string),而不是Foo(int)。这是因为默认参数值纯粹是一种语言特性,. net运行时对它们一无所知。(这也解释了为什么在c#中默认值必须是编译时常量)。

其他回答

重新排序枚举值

中断类型:源级/二进制级安静语义更改

受影响语言:全部

重新排序枚举值将保持源级兼容性,因为字面量具有相同的名称,但它们的序号索引将被更新,这可能导致某些类型的静默源级中断。

更糟糕的是,如果客户端代码没有根据新的API版本重新编译,就会引入无声的二进制级中断。Enum值是编译时的常量,因此任何对它们的使用都会被写入客户端程序集的IL中。这种情况有时尤其难以发现。

更改前的API

public enum Foo
{
   Bar,
   Baz
}

更改后的API

public enum Foo
{
   Baz,
   Bar
}

示例客户端代码,可以工作,但随后被破坏:

Foo.Bar < Foo.Baz

这种情况在实践中是非常罕见的,但当它发生时还是令人惊讶的。

添加新的非重载成员

Kind:源级中断或安静的语义更改。

受影响的语言:c#, VB

不受影响的语言:f#, c++ /CLI

更改前的API:

public class Foo
{
}

更改后的API:

public class Foo
{
    public void Frob() {}
}

被更改破坏的示例客户端代码:

class Bar
{
    public void Frob() {}
}

class Program
{
    static void Qux(Action<Foo> a)
    {
    }

    static void Qux(Action<Bar> a)
    {
    }

    static void Main()
    {
        Qux(x => x.Frob());        
    }
}

注:

这里的问题是由c#和VB中存在重载解析的lambda类型推断引起的。这里使用了一种有限形式的duck类型,通过检查lambda的主体对给定类型是否有意义来打破多个类型匹配的关系——如果只有一种类型产生可编译的主体,则选择该类型。

The danger here is that client code may have an overloaded method group where some methods take arguments of his own types, and others take arguments of types exposed by your library. If any of his code then relies on type inference algorithm to determine the correct method based solely on presence or absence of members, then adding a new member to one of your types with the same name as in one of the client's types can potentially throw inference off, resulting in ambiguity during overload resolution.

请注意,本例中的类型Foo和Bar在任何方面都没有关联,不是通过继承或其他方式。仅仅在一个方法组中使用它们就足以触发这种情况,如果这种情况发生在客户端代码中,则您无法控制它。

The sample code above demonstrates a simpler situation where this is a source-level break (i.e. compiler error results). However, this can also be a silent semantics change, if the overload that was chosen via inference had other arguments which would otherwise cause it to be ranked below (e.g. optional arguments with default values, or type mismatch between declared and actual argument requiring an implicit conversion). In such scenario, the overload resolution will no longer fail, but a different overload will be quietly selected by the compiler. In practice, however, it is very hard to run into this case without carefully constructing method signatures to deliberately cause it.

当我发现它时,这一点非常不明显,特别是考虑到接口的相同情况。这根本不是一个休息,但我决定把它包括在内,这是足够令人惊讶的:

将类成员重构为基类

Kind:不休息!

受影响的语言:无(即没有损坏)

更改前的API:

class Foo
{
    public virtual void Bar() {}
    public virtual void Baz() {}
}

更改后的API:

class FooBase
{
    public virtual void Bar() {}
}

class Foo : FooBase
{
    public virtual void Baz() {}
}

在整个更改过程中保持工作的示例代码(即使我预计它会中断):

// C++/CLI
ref class Derived : Foo
{
   public virtual void Baz() {{

   // Explicit override    
   public virtual void BarOverride() = Foo::Bar {}
};

注:

C++/CLI is the only .NET language that has a construct analogous to explicit interface implementation for virtual base class members - "explicit override". I fully expected that to result in the same kind of breakage as when moving interface members to a base interface (since IL generated for explicit override is the same as for explicit implementation). To my surprise, this is not the case - even though generated IL still specifies that BarOverride overrides Foo::Bar rather than FooBase::Bar, assembly loader is smart enough to substitute one for another correctly without any complaints - apparently, the fact that Foo is a class is what makes the difference. Go figure...

Visual Studio扩展NDepend在API Breaking Changes类别中提供了几个规则来检测二进制级别的中断。只有定义了NDepend基线,这些规则才会执行。

API Breaking Changes: Types: This rule warns if a type publicly visible in the baseline, is not publicly visible anymore or if it has been removed. Clients code using such type will be broken. API Breaking Changes: Methods: This rule warns if a method publicly visible in the baseline, is not publicly visible anymore or if it has been removed. Clients code using such method will be broken. Note that if a method signature gets changed the old method version is seen as removed and the new method version is seen as added, so a breaking change will be detected on the old method version. API Breaking Changes: Fields: This rule warns if a field publicly visible in the baseline, is not publicly visible anymore or if it has been removed. Clients code using such field will be broken. API Breaking Changes: Interfaces and Abstract Classes: This rule warns if a publicly visible interface or abstract class has been changed and contains new abstract methods or if some abstract methods have been removed. Clients code that implement such interface or derive from such abstract class will be broken. Broken serializable types: This rule warns about breaking changes in types tagged with SerializableAttribute. To do so, this rule searches for serializable type with serializable instance fields added or removed since the baseline. Notice that it doesn't take account of fields tagged with NonSerializedAttribute. Avoid changing enumerations Flags status: This rule matches enumeration types that used to be tagged with FlagsAttribute in the baseline, and not anymore. It also matches the opposite, enumeration types that are now tagged with FlagsAttribute, and were not tagged in the baseline. Being tagged with FlagsAttribute is a strong property for an enumeration. Not so much in terms of behavior (only the enum.ToString() method behavior changes when an enumeration is tagged with FlagsAttribute) but in terms of meaning: is the enumeration a range of values or a range of flags?

还有3个代码查询建议让用户浏览新的公共API元素:

新的公开可见类型。 新的公开可见方法。 新的公开可见字段。

API的改变:

添加[Obsolete]属性(你已经提到了属性;然而,当使用warning-as-error时,这可能是一个破坏性的更改。)

二进制级别突破:

Moving a type from one assembly to another Changing the namespace of a type Adding a base class type from another assembly. Adding a new member (event protected) that uses a type from another assembly (Class2) as a template argument constraint. protected void Something<T>() where T : Class2 { } Changing a child class (Class3) to derive from a type in another assembly when the class is used as a template argument for this class. protected class Class3 : Class2 { } protected void Something<T>() where T : Class3 { }

源级安静语义更改:

添加/删除/更改Equals(), GetHashCode()或ToString()的覆盖


(不知道这些适合哪里)

部署变更:

添加/移除依赖/引用 将依赖项更新到新版本 在x86、Itanium、x64或anycpu之间更改“目标平台” 在不同的框架安装上构建/测试(例如,在.Net 2.0盒子上安装3.5允许调用需要.Net 2.0 SP2的API)

引导程序/配置更改:

添加/删除/更改自定义配置选项(即App.config设置) 随着IoC/DI在当今应用程序中的大量使用,有必要为依赖于DI的代码重新配置和/或更改引导代码。

更新:

对不起,我没有意识到这是打破我的唯一原因是我在模板约束中使用它们。