简体   繁体   English

C#类向后兼容

[英]C# class backward compatibility

I have a NuGet package that is been used by some projects internally. 我有一个NuGet程序包,该程序已在内部被某些项目使用。 Now, while refactoring the project, I found several classes that were named inappropriately. 现在,在重构项目时,我发现了一些命名不当的类。 I want to know if there is any way in C# change the class names and not break anything. 我想知道C#中是否有任何方法可以更改类名而不破坏任何内容。

There is no way to rename identifiers without breaking other people code. 在不破坏其他人代码的情况下无法重命名标识符。 The best thing you can do is to leave clAssNaME identifier, provide new ClassName and mark clAssNaME as deprecated. 最好的办法是保留clAssNaME标识符,提供新的ClassName并将clAssNaME标记为已弃用。 Publish new release with documented and highlighted changelog. 发布新版本以及已记录并突出显示的变更日志。

When you're confident enough that most people managed to fix their code, delete the clAssNaME . 当您有足够的信心让大多数人设法修复他们的代码时,请删除clAssNaME

What you do depends on the extent of the changes. 您所做的取决于更改的程度。 If the types with the changed names are not simple types (ie. have a lot of behavior) or are used throughout your package it can be very difficult to create a copy of the type because you also have to ensure the code can use either type (for a while anyways). 如果名称更改的类型不是简单类型(即具有很多行为)或在整个包中使用,则创建该类型的副本可能非常困难,因为您还必须确保代码可以使用这两种类型(反正一段时间)。

A simpler solution might be to branch your package and increment the major version number in the new branch. 一个更简单的解决方案是分支您的软件包,并在新分支中增加主要版本号。 In the "new" branch: Update the type names, document the breaking changes, and push a release as a new version. 在“新”分支中:更新类型名称,记录重大更改,然后将发行版推送为新版本。 You can then maintain both branches until you see fit to stop work on the "older" branch. 然后,您可以维护两个分支,直到您认为适合停止“较旧”分支上的工作为止。 In the "older" branch you can also mark types as deprecated with a warning that in the future version they will have a changed name. 在“较旧”分支中,您还可以将类型标记为已弃用,并带有警告,指出将来的版本中它们的名称将更改。

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

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