如何为继承IComparable<IWeighted>的IWeighted接口提供默认公开实现?
问题
我定义了如下接口:
public interface IWeighted : IComparable<IWeighted> { int Weight { get; } // 我知道此部分不满足IComparable<>的契约,还会触发隐藏继承成员的警告 public int CompareTo(IWeighted other) { return Weight.CompareTo(other.Weight); } // 此部分满足契约,但会阻止实现类修改它或公开访问它 int IComparable<IWeighted>.CompareTo(IWeighted other) { return Weight.CompareTo(other.Weight); } // 此部分同样不满足IComparable<>的契约 public new int CompareTo(IWeighted other) { return Weight.CompareTo(other.Weight); } }
该接口继承了IComparable<IWeighted>,请问是否可以提供一个默认的公开实现,使其满足IComparable<>的契约?
示例用法及痛点
public struct WeightedTest : IWeighted { private int m_Weight; public int Weight => m_Weight; /* 应已有CompareTo的实现 */ /* 我知道下一行是编译器错误,仅用于示意 */ public int IComparable<IWeighted>.CompareTo(IWeighted other); } public struct WeightedTest2 : IWeighted { private RarityEnum m_Rarity; public int Weight => (LengthofRarityEnumValues) - (int)m_Rarity; /* 应已有CompareTo的实现 */ /* 我知道下一行是编译器错误,仅用于示意 */ public int IComparable<IWeighted>.CompareTo(IWeighted other); } public class LootTable { private List<IWeighted> m_Items; /* 其他与IWeighted无关的代码 */ public void Sort() { m_Items.Sort(); // 得益于IComparable,此代码可正常运行 } } public struct OtherIWeightedContainer : IComparable<OtherIWeightedContainer> { private WeightedTest m_Item; // 示例 /* 其他与WeightedTest无关的代码 */ public int CompareTo(OtherIWeightedContainer other) { /* 更高优先级的条件 */ return m_Item.CompareTo(other.m_Item); // 此代码无法运行 } }
目前IComparable和IWeighted的CompareTo方法存在矛盾:要么不满足契约,要么无法公开访问。导致实现IWeighted的类自身需要被比较时,编译器会报错,除非在每个实现类中重复编写相同的CompareTo方法。
解决方案
在C# 8.0及以上版本中,利用接口默认实现特性,可以同时满足IComparable<IWeighted>的契约要求,并且为实现类提供可直接调用的公开CompareTo方法,无需重复编写代码。
正确的接口实现
public interface IWeighted : IComparable<IWeighted> { int Weight { get; } // 显式实现IComparable<IWeighted>接口方法,严格遵守契约 int IComparable<IWeighted>.CompareTo(IWeighted other) { if (other == null) return 1; return Weight.CompareTo(other.Weight); } // 提供公开的默认CompareTo方法,调用显式实现的接口逻辑 public int CompareTo(IWeighted other) { return ((IComparable<IWeighted>)this).CompareTo(other); } }
方案说明
- 契约合规:显式实现
IComparable<IWeighted>.CompareTo完全符合接口契约要求,不会触发隐藏成员的警告,同时保证List<IWeighted>.Sort()这类依赖接口契约的代码可以正常运行。 - 公开访问:公开的
CompareTo默认实现让所有IWeighted实现类都拥有一个可直接调用的比较方法,解决了OtherIWeightedContainer中m_Item.CompareTo(other.m_Item)无法编译的问题。 - 可扩展性:如果实现类需要自定义比较逻辑,直接重写公开的
CompareTo方法即可,无需修改接口契约的实现。例如:
public struct CustomWeighted : IWeighted { public int Weight { get; set; } public int Priority { get; set; } // 重写公开的CompareTo,实现自定义比较逻辑 public int CompareTo(IWeighted other) { if (other is not CustomWeighted customOther) return Weight.CompareTo(other.Weight); // 先按Weight比较,Weight相同时按Priority排序 int weightCompare = Weight.CompareTo(customOther.Weight); return weightCompare != 0 ? weightCompare : Priority.CompareTo(customOther.Priority); } }
验证示例
修改后的WeightedTest无需手动实现CompareTo:
public struct WeightedTest : IWeighted { private int m_Weight; public int Weight => m_Weight; // 接口默认实现已提供CompareTo方法,无需重复编写 }
此时OtherIWeightedContainer中的return m_Item.CompareTo(other.m_Item);可以正常编译运行,LootTable.Sort()也能正常对List<IWeighted>进行排序。
内容的提问来源于stack exchange,提问作者user3915050
相关产品推荐
相关产品推荐

