简体   繁体   English

如何使这种方法通用?

[英]How to make this method generic?

How can I make this function generic? 如何使该函数通用? At the moment this method retrieves a list of type item. 目前,此方法检索类型项的列表。 What I want to do is to call on this method, with a generic datatype, like Item, Category or Group. 我想做的是使用通用数据类型(例如Item,Category或Group)调用此方法。 All of these has the same property name. 所有这些都具有相同的属性名称。

How can I do this? 我怎样才能做到这一点?

In logic / service layer with reference to the Data Layer: 在逻辑/服务层中参考数据层:

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 ;
    }
}

In dataaccess layer 在数据访问层

Datatype1.cs Datatype1.cs

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

Datatype2.cs Datatype2.cs

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

Datatype3.cs Datatype3.cs

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

As all of your types have the same property, you should have a common base-class or interface for them. 由于所有类型都具有相同的属性,因此您应该具有一个通用的基类或接口。 Then you can easily add a generic constraint to your method: 然后,您可以轻松地向您的方法添加通用约束:

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,
        });
    }

    // ...
}

with

interface MyInterface
{
    // the common properties
}

and

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

Apart from this I can´t see why your method is async at all, as there´s nothing that can be awaited here. 除此之外,我根本看不出为什么您的方法是async的,因为这里没有什么可以等待的。

Your code is async although there is no calls to async. 您的代码是异步的,尽管没有异步调用。 It returns a "message" although there is no reference to any "message" variable. 尽管没有引用任何“ message”变量,但它返回“ message”。 The code is pretty unreadable so its hard to know exactly what you want. 该代码非常难以阅读,因此很难确切知道您想要什么。

But you need to wrap the method in a generic class. 但是您需要将方法包装在通用类中。 Maybe something like this is what you want. 也许像这样是您想要的。

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;
    }
}

Wrap your method inside a class which can accept T type (where T : class, new()). 将您的方法包装在可以接受T类型的类中(其中T:class,new())。 and add your method which accept the T - type parameter and return T-type of object. 并添加接受T-type参数并返回对象T-type的方法。

return message can be newList. 返回消息可以是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; } 
}

If all those classes have the same method named Handler do something like you define a parameter T on the interface level, and your methods can use this parameter in their prototype, so any class that will be implementing this interface will naturally implement the parameter T within its own methods. 如果所有这些classes都具有名为Handler的相同方法,则可以像在接口级别上定义参数T那样进行操作,并且您的方法可以在其原型中使用此参数,因此将要实现此接口的任何类自然都将在其中实现参数T它自己的方法。

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

and then do: 然后执行:

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,
          });
      }
    // ...
    }
}

or totally if you want make that Handler as a generic method you can do something like: 或完全如果您想将Handler作为通用方法,则可以执行以下操作:

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

and

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

and call it like(just for example) : 并称其为(例如):

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