具有不同类型参数的接口方法
我正在构建一组过滤器类别,这些级别都将具有相同的方法“ applyfilter”。
我应该如何定义包含应用过滤器的接口?唯一的问题是,应用过滤器可以进行第二种类型的参数,例如int,字符串,列表。一些伪代码。
当前接口方法:
Data ApplyFilter(input-data, object value);
示例:
public *data* ApplyFilter(input-data, ***string color***) {
// Do something with to data with the color string
}
public *data* ApplyFilter(input-data, ***List<int> size***) {
// Do something with to data with the size list
}
如果我将参数二的类型定义为“对象”。我可以在应用程序函数中进行一些验证。 如下所述:检查是否是字典或列表更好的方法?
I'm constructing a set of filter-classes which will all have the same method 'Applyfilter'.
How should I define the interface which contains apply filter? The only issue is that apply filter can take a second argument of various types e.g. int, string, Lists. Some pseudo code.
Current Interface method:
Data ApplyFilter(input-data, object value);
Example:
public *data* ApplyFilter(input-data, ***string color***) {
// Do something with to data with the color string
}
public *data* ApplyFilter(input-data, ***List<int> size***) {
// Do something with to data with the size list
}
If I defined the type of argument two as an 'object'. I can do some validation within the ApplyFilter function.
As mentioned here: Check if Object is Dictionary or List but is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于集中式代码,您可以创建一个过滤器属性类
,然后创建一个
applyfilter
方法,该方法将此类作为参数,现在您有了一种过滤器方法来避免复制代码,并具有灵活性来添加新的可选过滤器容易地。
For centralized code , you can create a filter properties class
Then create an
ApplyFilter
method that takes this class as an argumentNow you have one filter method to avoid duplicating code, and have the flexibility to add new optional filters easily.
一种方法是使用
为了景象:
An approach would be to use Generic
For exemple: