如何将类实体作为参数传递?

时间:2016-03-31 18:12:36

标签: c# .net

我需要将类实体传递给另一个类,这个方法在第二类

 public void AutoComplet(TextEdit text_searche, Class entity here)
 {
     AutoCompleteStringCollection collection = new AutoCompleteStringCollection();

     ArrayList fo = new ArrayList();

     fo = t.pms_product_ALLSelectlabo();

     foreach (Class entity here pr in fo)
     {
         collection.Add(pr.blabla);
     }

     text_searche.AutoCompleteSource = AutoCompleteSource.CustomSource;
     text_searche.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
     text_searche.AutoCompleteCustomSource = collection;
 }

在第一节课中我需要这样做

optionDb.AutoComplet(searche, Class entity here);

我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果您正在使用类型,最简单的方法是创建函数generic

 public void AutoComplet<T>(TextEdit text_searche)
 {
     AutoCompleteStringCollection collection = new AutoCompleteStringCollection();

     ArrayList fo = new ArrayList();

     fo = t.pms_product_ALLSelectlabo();

     foreach (T pr in fo.OfType<T>())
     {
         collection.Add(pr.blabla);
     }

     text_searche.AutoCompleteSource = AutoCompleteSource.CustomSource;
     text_searche.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
     text_searche.AutoCompleteCustomSource = collection;
 }

并通过提供通用参数来调用它:

optionDb.AutoComplet<Class entity here>(searche);

我还建议使用强类型集合,而不是古老的ArrayList