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

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

问题描述

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

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

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