简体   繁体   English

C# generics 类型解析

[英]C# generics type resolving

I am writing a popup window service in for our mvvm application.我正在为我们的 mvvm 应用程序编写一个弹出式 window 服务。

I have wrote this method in the popup controller我在弹出窗口 controller 中写了这个方法

void ShowDialogWithResult<TView, TViewModel, TResult>(Action<TResult, WindowClosedEventArgs> callbackAction)
            where TView : FrameworkElement, IPopupContent<TViewModel>
            where TViewModel : IResultViewModel<TResult>;

As you can see to show a popup window with view model and result the view must implement IPopupContent<TViewModel> interface, and in its turn the view model must implement interface IResultViewModel<TResult>如您所见,显示带有视图 model 的弹出窗口 window 和结果视图必须实现IPopupContent<TViewModel>接口,然后视图 model 必须实现接口IResultViewModel<TResult>

So we have a chain of types started from TView->TViewModel->TResult所以我们有一个从 TView->TViewModel->TResult 开始的类型链

the call to such method looks like this:对此类方法的调用如下所示:

 _childWindowController
                .ShowDialogWithResult<AddNationalityPopup,AddNationalityPopupModel, AddNationalityResult>(
                    (result, a) =>
                    {
                        if (a.DialogResult.HasValue && a.DialogResult.Value)
                        {
                            if (result.NationalityCountryId.HasValue)
                            {
                                Background.NationalityCountryId = result.NationalityCountryId.Value;
                                Background.NationalityDescription = result.NationalityDescription;
                            }
                        }
                    });

As you can see I am forced to pass all three type parameters for compiler to generate correct method.如您所见,我被迫将所有三个类型参数传递给编译器以生成正确的方法。

And this code makes me sad.这段代码让我很难过。 How can I reduce the amount of type parameters required by the call and still get type safety.如何减少调用所需的类型参数数量并仍然获得类型安全。 I can't develop any valid solution.我无法开发任何有效的解决方案。

There's no way of reducing these generic type parameters in this call, but you can use inheritance in order to specialize it overloading this method.在此调用中无法减少这些泛型类型参数,但您可以使用 inheritance 来专门重载此方法。

For example, if there're a lot of calls to .ShowDialogWithResult<AddNationalityPopup,AddNationalityPopupModel, ...> , you can inherit your controller and add an overload like .ShowDialogWithResult<TResult> .例如,如果有很多调用.ShowDialogWithResult<AddNationalityPopup,AddNationalityPopupModel, ...> ,您可以继承您的 controller 并添加类似.ShowDialogWithResult<TResult>的重载。

Take 2:取2:

Another approach like this would be adding these method's generic type parameters to the controller class and using inversion of control, it can be instantiated with the appropiate type arguments freeing the caller of providing them in .ShowDialogWithResult(...) .像这样的另一种方法是将这些方法的泛型类型参数添加到 controller class 并使用控制反转,它可以用适当的类型.ShowDialogWithResult(...)实例化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM