【发布时间】:2014-09-17 02:38:03
【问题描述】:
我有 16 个带有两个参数的方法,两个参数中的每一个都可以是“插入”或“删除”,它们都实现了 IFragment。我也有四个这样的辅助函数:
static IFragment[] IntroduceAntecedent(IFragment on, IFragment item) {
bool onIsInsertion = on is Insertion;
bool itemIsInsertion = item is Insertion;
if (onIsInsertion) {
if (itemIsInsertion) {
return IntroduceAntecedent((Insertion) on, (Insertion) item);
} else {
return IntroduceAntecedent((Insertion) on, (Deletion) item);
}
}
else {
if (itemIsInsertion) {
return IntroduceAntecedent((Deletion)on, (Insertion)item);
} else {
return IntroduceAntecedent((Deletion)on, (Deletion)item);
}
}
}
它只是确定实际类型并调用适当的重载。有没有更清洁的方法来做到这一点?换一种说法,我可以用衍生较少类型的对象调用衍生较多的函数重载吗?
编辑: IntroduceAntecedent 重载的签名
static IStringTransform[] IntroduceAntecedent(Deletion lhs, Deletion rhs)
static IStringTransform[] IntroduceAntecedent(Deletion lhs, Insertion rhs)
static IStringTransform[] IntroduceAntecedent(Insertion lhs, Deletion rhs)
static IStringTransform[] IntroduceAntecedent(Insertion lhs, Insertion rhs)
【问题讨论】:
-
您能否也分享一下 IntroduceAntecedent 的外观。这可以帮助您真正了解您要做什么并简化它。
-
我已经添加了签名。
-
如果我理解正确,您应该能够将 4 个 IntroduceAntecedent 方法定义为 Extension Methods,并提供签名,然后简单地调用它们得到正确的调用
标签: c# inheritance overloading