c# - 我怎样才能在这里提取代码重复?

标签 c# .net refactoring dry

我试图在提取方法中提取通用代码模式,但找不到适合 Presenter 类型的正确类型。有什么帮助吗?

public bool CanGotoHome 
{ 
    get { return !(CurrentPresenter is IHomePresenter) && IsLoggedIn; } 
}
public bool CanGotoImportanceOfAimsAndObjectives 
{ 
    get { return !(CurrentPresenter is IImportanceOfAimsAndObjectivesPresenter) && IsLoggedIn; } 
}
public bool CanGotoGotoAimsAndObjectives 
{ 
    get { return !(CurrentPresenter is IAimsAndObjectivesPresenter) && IsLoggedIn; } 
}

最佳答案

使用泛型

private bool SomeFuncName<T>()
{
    return !(CurrentPresenter is T) && IsLoggedIn;
}

用法:

public bool CanGotoGotoAimsAndObjectives { 
    get { return SomeFuncName<IAimsAndObjectivesPresenter>(); } 
}

关于c# - 我怎样才能在这里提取代码重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1898645/

相关文章:

c# - Entity Framework 5 不清除导航属性

c# - 动态改变面板的大小

c# - 在打印预览中绘制边框但不应打印边框 C#

c# - 使用许多 if 和重复逻辑重构代码以从不同控件类中提取值的最佳方法是什么

python - 如何在 Python 中清理这个选择排序函数?

Ant exec 重构

c# - 如何将图像绘制到图片框?

c# - mvc 2中的图像上传和预览

.net - FitNesse App.Config

.net - 如何在保留书签的同时向现有 pdf 添加附加页面? (PDFSharp 等)