繁体   English   中英

如何使这种方法通用?

[英]How to make this method generic?

如何使该函数通用? 目前,此方法检索类型项的列表。 我想做的是使用通用数据类型(例如Item,Category或Group)调用此方法。 所有这些都具有相同的属性名称。

我怎样才能做到这一点?

在逻辑/服务层中参考数据层:

public class TaskHandler : ITaskHandler
{
     public async Task<List<newDataType>> Handler(List<Item>() items)
    {
        var newList = new List<newDataType>(); 
        foreach (var item in items)
        {
            newList.Add(new Item
            {
                ID = item.ID,
                Name = item.Status,
                Retrieved = DateTime,
            });
        } 
        return newList ;
    }
}

在数据访问层

Datatype1.cs

public class Datatype1
{
    public int ID{ get; set; }
    public string Name{ get; set; }
    public string Group{ get; set; }
}

Datatype2.cs

public class Datatype2
{
    public int ID{ get; set; }
    public string Name{ get; set; }
    public string Group{ get; set; }
}

Datatype3.cs

public class Datatype3
{
    public int ID{ get; set; }
    public string Name{ get; set; }
    public string Group{ get; set; }
}

由于所有类型都具有相同的属性,因此您应该具有一个通用的基类或接口。 然后,您可以轻松地向您的方法添加通用约束:

public async Task<List<T>> Handler<T>(List<Item> items) where T: MyInterface, new()
{
    var newList= new List<T>();
    foreach (var item in items)
    {
        newList.Add(new T
        {
            ID = item.ID,
            Name = item.Status,
            Retrieved = DateTime,
        });
    }

    // ...
}

interface MyInterface
{
    // the common properties
}

class Item : MyInterface { ...}
class Category : MyInterface { ...}
class Group : MyInterface { ...}

除此之外,我根本看不出为什么您的方法是async的,因为这里没有什么可以等待的。

您的代码是异步的,尽管没有异步调用。 尽管没有引用任何“ message”变量,但它返回“ message”。 该代码非常难以阅读,因此很难确切知道您想要什么。

但是您需要将方法包装在通用类中。 也许像这样是您想要的。

public class Foo<T> where T : new()
{
    public IEnumerable<T> Handler(IEnumerable<T> items)
    {
        var list = new List<T>();

        foreach (var item in items)
        {
            list.Add(new T
            {
                ID = item.ID,
                Name = item.Status,
                Retrieved = DateTime.Now,
            });
        }

        return list;
    }
}

将您的方法包装在可以接受T类型的类中(其中T:class,new())。 并添加接受T-type参数并返回对象T-type的方法。

返回消息可以是newList。

    public class Item
    {
      public int ID { get; set; }
      public string Status { get; set; }
    }

    public interface IRepositoryClass
    {
      int ID { get; set; }
      string Name { get; set; }
      DateTime Retrieved { get; set; }
    }

    public class YourRepositoryClass<T> where T : IRepositoryClass, new()
    { 
     public async Task<IEnumerable<T>> Handler(List<Item> items)
     { 
       var newList= new List<T>();
       foreach (var item in items)
        {
         newList.Add(new T
         {
            ID= item.ID,
            Name= item.Status,
            Retrieved= DateTime,
         });
      }
   return newList; } 
}

如果所有这些classes都具有名为Handler的相同方法,则可以像在接口级别上定义参数T那样进行操作,并且您的方法可以在其原型中使用此参数,因此将要实现此接口的任何类自然都将在其中实现参数T它自己的方法。

interface IBaseInterface<T>
{ 
   Task<List<T>> Handler(List<T> items);
    // the common properties
}

然后执行:

public class A : IBaseInterface<A>
{
   public A() { }

   public async Task<List<A>> Handler(List<A> items)
   {
      var newList= new List<A>();
      foreach (var item in items)
      {
          newList.Add(new A
          {
              ID = item.ID,
              Name = item.Status,
              Retrieved = DateTime,
          });
      }
    // ...
    }
}

或完全如果您想将Handler作为通用方法,则可以执行以下操作:

public interface IBaseInterface
{
  //common props
} 
public class DataType1 : IBaseInterface
{
    public DataType1() { }   
}

public class Common
{
    public async Task<List<T>> Handler<T>(List<T> items) where T : IBaseInterface
    {
        var newList = new List<T>();
        ....
    }
}

并称其为(例如):

public class Consumer
{
    public void Call()
    {
        var param1 = new List<DataType1>();
        var t = new Common().Handler<DataType1>(param1).Result;
    }
}

暂无
暂无

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

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