如何让方法接受两个类型的数据作为参数?参数、两个、类型、方法

2023-09-06 17:55:28 作者:我为你画地为牢

我有一个方法,接受一个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)

的其余部分是完全一样的。

The rest is exactly the same.

我觉得有一种方法来合并这一点。

I think there's a way to merge this.

推荐答案

这两个类实现ICollection接口,所以如果你的共同code'将致力于对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.