简体   繁体   English

将可以为两种不同类型的对象传递给C#中的函数

[英]Passing an object that can be two different types, to a function in c#

I want to have a function that receives either an sqlDataAdapter or a regular oleDataAdapter as on object parameter let's call it dbAdapter. 我想要一个接收sqlDataAdapter或常规oleDataAdapter的函数作为对象参数,让我们将其称为dbAdapter。 I then want to use dbAdapter to access rows that are associated with whichever data adapter was passed to the function. 然后,我想使用dbAdapter来访问与任何传递给该函数的数据适配器相关联的行。 Is this possible? 这可能吗? I wanted to avoid passing a parameter to tell which one to use and then have an if statement with a set of almost identical code for each case. 我想避免传递参数来告诉使用哪个参数,然后为每个案例使用一个if语句,该语句包含一组几乎相同的代码。 So, hypothetically, the function would be something like: 因此,假设该函数类似于:

private void LoadRow(object dbAdapter, int theRow)
  {
     txtID.Text = dbAdapter.Rows[theRow]["ID"].ToString();

  }

and the call would look something like: 呼叫看起来像这样:

LoadRow(sqlDbAdapter, 1);

Or 要么

LoadRow(oleDbAdapter, 1);

Where sqlDbAdapter is declared as an SqlDataAdapter and oleDbAdapter is declared as an OleDataAdapter. 其中sqlDbAdapter声明为SqlDataAdapter,而oleDbAdapter声明为OleDataAdapter。

I guess I could pass the function a value indicating which one to use and then an if statement to create which ever data adapter to use to the same variable name....like: 我想我可以给该函数传递一个值,该值指示要使用的那个,然后通过if语句来创建要用于同一变量名的数据适配器。

if (adapterType = "sql")
  {
     SqlDataAdapter dbAdapter = new SqlDataAdapter;
  }
else
  {
     OleDataAdapter dbAdapter = new OleDataAdapter;
  }

I think all the commands and other parameters of each adapter type are the same.... But I would like to pass the object by reference if that is possible... 我认为每种适配器类型的所有命令和其他参数都相同。...但是如果可能的话,我想通过引用传递对象...

Please give me your ideas and suggestions.... 请给我您的想法和建议。

Thanks! 谢谢!

Valhalla 瓦尔哈拉

If you have to do it this way, I'd use extension methods: 如果您必须采用这种方式,则可以使用扩展方法:

public static string LoadRow(this SqlDataAdapter adapter, int theRow)
{
    //return something you need
}

public static string LoadRow(this OleDataAdapter adapter, int theRow)
{
    //return something you need
}

Usage would be like: 用法如下:

myOleDataAdapter.LoadRow(9);
mySqlDataAdapter.LoadRow(5;

Edit - To answer the critics, if IDbDataAdapter has everything in it that you need, you can do this instead because both of the adapters you are trying to use implement it. 编辑 -要回答批评者,如果IDbDataAdapter包含了您需要的所有内容,则可以执行此操作,因为您要使用的两个适配器都实现了它。

public static string LoadRow(this IDbDataAdapter adapter, int theRow)
{
    //return something you need
}

A solution would be: 一个解决方案是:

private void LoadRow(DbDataAdapter dbAdapter, int theRow){
    DataSet dataSet = new DataSet();
    dbAdapter.Fill(dataSet);
    txtID.Text = dataSet.Tables["myTable"].Rows[theRow]["ID"].ToString();
}

But use it carefully since the .Fill() method could result slow if used many times on a large data set. 但请谨慎使用,因为如果在大型数据集上多次使用.Fill()方法可能会导致速度变慢。 My advice is to fill the data set just once and use it as much as possible and then release it, or if it isn't meant to become too large, keep it in memory and refresh it when you think it's apportune. 我的建议是只填充一次数据集,并尽可能多地使用它,然后释放它;或者如果它不打算变得太大,则将其保留在内存中,并在您认为合适时刷新它。

一个简单的方法是重载该方法,只需两次创建它,一个对象是SqlDataAdapter,另一次将其创建为OleDataAdapter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM