如何创建具有可变参数/不同的方法签名法的界面? [英] How to create method interface with variable parameters / different method signatures?

查看:130
本文介绍了如何创建具有可变参数/不同的方法签名法的界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个接口为通用类,但实现类可以有不同的参数。

I'm trying to create an interface to a common class, but the implementation classes can have different parameters.

例如。

public interface IViewModel
{
    //...
    void ResetReferences(); 
}

// and then, in my class implementations, something like this:
public class LocationViewModel : IViewModel
{
    public void ResetReferences(List<StateProvinces> stateProvinces) //...
}

public class ProductViewModel : IViewModel
{
    public void ResetReferences(List<Color> colors, List<Size> sizes) //...
}

因此​​注意到我想对RestReferences命名约定规范。我是pretty的肯定,我不能这样做,但有一个可以工作的设计模式?例如在我的界面,类似下面?

So notice that I want to standardize on the "RestReferences" naming convention. I'm pretty sure I can't do this, but is there a design pattern that could work? e.g. in my interface, something like below?

// variable parameters
void ResetReferences(params object[] list); 

但后来我怎么做我做的类型检查,或具有呼叫,我想实际的方法签名等?

But then how do I make I do type checking or having it call the actual method signature that I want, etc?

也许一个接口是错误的东西使用?也许只是一个基类和一些编码约定?

Maybe an interface is the wrong thing to use? Maybe just a base class and some coding conventions?

谢谢

推荐答案

与实现相关的接口的对象替换您的args列表:

Replace your args lists with objects that implement a related interface:

public interface IViewModel
{
    //...
    void ResetReferences(IResetValues vals); 
}

我要补充一点,国际海事组织,ResetReferences()不应该带任何参数...它应该重新设置一些默认值,这将是具体到个人型(S)实现你的接口。复位是的,意思是,对我来说,恢复到初始状态...添加的args意味着你可以控制的。

I should add that, IMO, "ResetReferences()" should not take an argument...it should reset to some default value that would be specific to the individual type(s) that implement your interface..."Reset" being the word that means, to me, "restore to initial state"...adding args implies that you can control that.

这篇关于如何创建具有可变参数/不同的方法签名法的界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆