转换派生类型和动态最佳实践

Wal*_*ung 3 c# mvp c#-4.0

我正在使用C#4.0开发一个项目.我有几个presenter类,它们都继承自同一个基类.这些演示者类中的每一个都有一个"当前"对象,该对象特定于每个演示者,但它们都有一个共同的继承类,当然与演示者分开.

在可怕的伪代码中:

class ApplicationPresenter inherits BasePresenter
   Pubilc PersonApplication Current (inherited from Person)
class RecordPresenter inherits BasePresenter
   Public PersonRecord Current (inherited from Person)
class InquiryPresenter inherits BasePresenter
   Public PersonInquiry Current (inherited from Person)
Run Code Online (Sandbox Code Playgroud)

...等等

有没有办法让它可以从任何这些中调用"当前",而不需要类型检测,但是它是否与最佳实践保持一致?

我认为最好的选择就是让它成为一个动态的,因为我知道无论我传递给它的是什么,都会有"当前"并且这样做.那是对的吗?

或者有一种方法可以创建:

class BasePresenter
   Public Person Current
Run Code Online (Sandbox Code Playgroud)

并适当地进行演员表演?

我知道有很多方法可以解决这个问题,但我正在寻找干净和适当的方法.

谢谢!

sll*_*sll 5

将泛型类型参数添加到基本演示者,以便任何派生的具体演示者都能够指定自定义当前项目类型:

public abstract class PresenterBase<TItem>
{
   public TItem Current { get; private set; }
}

// now Current will be of PersonApplication type
public sealed class ApplicationPresenter : PresenterBase<PersonApplication>
{
}

// now Current will be of PersonRecord type
public sealed class RecordPresenter : PresenterBase<PersonRecord>
{
}
Run Code Online (Sandbox Code Playgroud)