如何让方法接受两种类型的数据作为参数? [英] How to let a method accept two types of data as argument?

查看:33
本文介绍了如何让方法接受两种类型的数据作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,接受一个哈希表(是的,我知道,它已经过时了..)作为参数:

I have a method, accepting a Hashtable (yes I know, it's outdated..) as argument:

public static LuaTable HashtableToLuatable(Hashtable t, int depth = 1)

这可以正常工作.

现在我也想接受一个 ArrayList 作为第一个参数,所以你可以让 't' 拥有 Hashtable 和 ArrayList 的值.目前我已经复制粘贴了该方法两次,如下所示:

Now I'd like to accept an ArrayList as first parameter as well, so you can let 't' have the value of both a Hashtable and an ArrayList. Currently I have copy-pasted the method two times, like this:

public static LuaTable ArraylistToLuatable(ArrayList t, int depth = 1)

其余的完全一样.

我认为有一种方法可以合并它.

I think there's a way to merge this.

推荐答案

两个类都实现了 ICollection 接口,因此如果您的通用代码"将与 ICollection 接口的定义相悖,您可以将其用于您的参数类型.

Both classes implement the ICollection interface, so if your 'common code' will work against the definition of the ICollection interface, you could use that for your parameter type.

这篇关于如何让方法接受两种类型的数据作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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