通过操作/委托在泛型对象上调用C#方法(kotlin示例)

通过操作/委托在泛型对象上调用C#方法(kotlin示例),c#,generics,kotlin,action,C#,Generics,Kotlin,Action,我想在C#中调用泛型对象的方法调用。我似乎不知道该怎么做。我将发布一个kotlin示例,说明我是如何在android应用程序中实现MVP模式的 基本演示者通用实现: interface IBasePresenter<in T> { fun takeView(view: T) fun dropView() } class BasePresenter<T> : IBasePresenter<T> { private var view: T

我想在C#中调用泛型对象的方法调用。我似乎不知道该怎么做。我将发布一个kotlin示例,说明我是如何在android应用程序中实现MVP模式的

基本演示者通用实现:

interface IBasePresenter<in T> {
    fun takeView(view: T)
    fun dropView()
}

class BasePresenter<T> : IBasePresenter<T> {
    private var view: T? = null

    final override fun takeView(view: T) {
        this.view = view
    }

    final override fun dropView() {
        view = null
    }

    fun onView(action: T.() -> Unit) {
        if (view != null) {
            action.invoke(view!!) // Magic :-)
        }
    }
}
interface IMyView {
    fun doSomeRendering(int width, int height)
}

interface IMyPresenter : IBasePresenter<IMyView> {
    fun onButtonClicked()
}
void OnView(Action<TView> action) => action(_view)
OnView(view => view.DoSomeRendering(800, 400))
这能像在科特林一样用C#完成吗? 我所需要的只是能够在具体的presenter实现中执行以下调用

onView { doSomeRendering(800, 400) }

这样,我就可以在BasePresenter中保持视图的私有性,而不会将其公开给具体的实现。

因此我想出了如何做到这一点。以下代码用于C#:

基本演示者实现:

interface IBasePresenter<in T> {
    fun takeView(view: T)
    fun dropView()
}

class BasePresenter<T> : IBasePresenter<T> {
    private var view: T? = null

    final override fun takeView(view: T) {
        this.view = view
    }

    final override fun dropView() {
        view = null
    }

    fun onView(action: T.() -> Unit) {
        if (view != null) {
            action.invoke(view!!) // Magic :-)
        }
    }
}
interface IMyView {
    fun doSomeRendering(int width, int height)
}

interface IMyPresenter : IBasePresenter<IMyView> {
    fun onButtonClicked()
}
void OnView(Action<TView> action) => action(_view)
OnView(view => view.DoSomeRendering(800, 400))

因此,视图不再需要在base presenter中进行保护,并且可以是私有的。

对不起,我上周末才开始学习Kotlin。我不确定
(action:T.()->Unit)
在C中的意思(特别是
T.()
),你想做一些类似
void-OnView(action-action){action?(view);}
的事情吗?