[英]Why some structs inherit interfaces, but do not implement all of their members?
在.NET Core中进行编程时,偶然发现了一个名为StringValues
的结构。 它来自Microsoft.Extensions.Primitive
程序集,并保存在同一名称空间下。 它的源代码可以在GitHub上找到 。
我注意到它继承了几个接口:
IList<string>
ICollection<string>
IEnumerable<string>
IEnumerable
IReadOnlyList<string>
IReadOnlyCollection<string>
IEquatable<StringValues>
IEquatable<string>
IEquatable<string[]>
我会说,对于一个结构来说,这是相当多的。 当我查看它的方法时(在Visual Studio定义查看器中),我注意到它提供的奇怪程度。 例如,我没有看到基本的ICollection<T>
方法,它们是Add()
, Clear()
或Remove()
。
当我试图明确地将一个StringValues
实例StringValues
为ICollection<string>
并使用Add()
:
var stringValues = new StringValues("Foo");
var stringCollection = stringValues as ICollection<string>;
stringCollection.Add("Bar");
我最终在Add()
方法调用时抛出了NotSupportedException
:
NotSupportedException:不支持指定的方法。
所以我做了一个源代码的旅程(开源的宝贵优势)来看看发生了什么! 然后我看到:
void ICollection<string>.Add(string item)
{
throw new NotSupportedException();
}
void IList<string>.Insert(int index, string item)
{
throw new NotSupportedException();
}
bool ICollection<string>.Remove(string item)
{
throw new NotSupportedException();
}
void IList<string>.RemoveAt(int index)
{
throw new NotSupportedException();
}
void ICollection<string>.Clear()
{
throw new NotSupportedException();
}
这种编程方式有什么意义? 我总是这样,通过继承接口,我宣称特定类型可以与继承接口一起使用。
在这种情况下,似乎这个结构不打算在公共场合使用? 我在查看已实现的Contains()
方法时得到的这种感觉,但仍标记为内部:
bool ICollection<string>.Contains(string item)
{
return IndexOf(item) >= 0;
}
因为这样做通常很有用 ,只要调用者只调用可用的方法。 他们可以检查像IList.IsReadOnly
这样的IList.IsReadOnly
来预测这个 - 如果返回false
,他们应该知道不要期望Add
等工作。
将结构不可变类型传递给历史上采用可变API的API(例如IList
)可能很有用 ,因为相同的接口还提供其他有用的功能 。
类似地, Stream
有CanRead
和CanWrite
类的东西,如果返回false
,你可以期望相应的读/写API失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.