我有这样的事情:
// This gets implemented by plugin authors to get callbacks about various things.
public interface ExternalPlugin
{
// This gets called by the main application to tell the plugin some data
// is available or similar.
void DoStuff(SomeDataBlob blob);
}
// Data blob for v1 of API
public class SomeDataBlob
{
internal SomeDataBlob(string prop) { Prop = prop; }
// Some piece of data that V1 plugins need
public string Prop { get; private set; }
}
// FUTURE!
// Data blob API v2 of API
public class SomeDataBlobV2 : SomeDataBlob
{
// Can be passed to clients expecting SomeDataBlob no problem.
internal SomeDataBlobV2(string prop, string prop2) :base(prop) { Prop2 = prop2; }
// Some piece of data that V2 plugins need. V2 plugins can cast to this from
// SomeDataBlob, but still can load successfully into older versions that support
// only V1 of the API
public string Prop2 { get; private set; }
}
我必须SomeDataBlob
公开,以便它可以用作公共接口方法的成员ExternalPlugin.DoStuff
。但是,我不想让客户端从该类继承,从而容易受到脆弱的基类问题的影响。(该类的所有衍生物都应保存在同一个程序集中)
标记类sealed
太过分了,因为我认为删除sealed
是一个破坏性的 API 更改;即使不是这样,一旦我交付SomeDataBlobV2
客户仍然可以做错事并SomeDataBlob
直接继承。
有没有办法强制执行这种模式?