[英]Should I use an implicit conversion to my return type, to aid in debugging?
I have the following method in my API 我的API中有以下方法
public bool CheckSignature(string data, string key)
{ ... }
In the event that I return false (for an invalid signature), I'd like the ability to cleanly get the debug data without changing all my callers code. 如果我返回false(对于无效的签名),我希望能够在不更改所有调用者代码的情况下干净地获取调试数据。
If I was returning an object, I would simply add a debug type like this 如果我要返回一个对象,则只需添加这样的调试类型
public class SignatureResult
{
public bool Validation {get;set;}
public SpecialDebugObject Debug {get;set;}
}
Is it a bad idea for me to change my API so that it returns a SignatureResult
but with an implicit conversion to bool so the existing clients don't break? 对我来说,更改API以使其返回SignatureResult
但隐式转换为bool以使现有客户端不会中断,是否是个坏主意?
Would this be OK to do for a new API? 对新的API可以这样做吗?
Existing clients will break, this is a breaking change. 现有的客户会破产,这是一个重大的变化。
The code written against CheckSignature
expects a bool
to be returned. 针对CheckSignature
编写的代码期望返回bool
。 Even if there's an implicit conversion between your type and bool, the compiled IL is missing the proper conversion instruction. 即使您的类型和布尔之间存在隐式转换,编译后的IL也缺少正确的转换指令。
This is analogous to changing a field to be a property instead: you lose binary compatibility . 这类似于将字段更改为属性: 您失去了二进制兼容性 。
Would this be OK to do for a new API? 对新的API可以这样做吗?
I wouldn't. 我不会 You're exposing debugging logic. 您正在公开调试逻辑。
Here's a good guide for what's considered a breaking change in .NET: A definitive guide to API-breaking changes in .NET . 对于被认为是.NET重大更改的人,这是一个很好的指南:.NET 中API重大更改的权威性指南 。 As you can see, method signatures are very sensible. 如您所见,方法签名非常明智。 Changing the return type, and even the argument's names are considered breaking changes. 更改返回类型,甚至更改参数名称都被视为重大更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.