如何让一个方法接受两种类型的数据作为参数? [英] How to let a method accept two types of data as argument?
问题描述
我有一个方法,接受一个Hashtable(是的,我知道,它已经过时了..)作为参数:
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屋!