简体   繁体   English

如何将 DTO 对象转换为通用 T 类型

[英]How to cast DTO object to Generic T type

i have this peace of code where i am returning a list of objects.我有这个代码的和平,我正在返回一个对象列表。 but the return type is not allowing to return.但返回类型不允许返回。 this my interface class这是我的接口类

interface SEC_Interface<T>
    {
        bool Add(T data);
        bool Update(T data);

        bool Delete(T data);

        List<T> GetAll();

        T GetById(int id);

        List<T> Search(string SearchTerm);
    }

Here is the method of class.My class is also generic type class and implementing this interface which is also generic.这是类的方法。我的类也是泛型类,并实现了这个也是泛型的接口。

public List<T> GetAll()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(CS))
                {
                    DataSet ds = SqlHelper.ExecuteDataset(conn, "SEC_GetAllUsers", null);

                    List<DTO_GetAll> result = ds.Tables[0].AsEnumerable()
                        .Select(dataRow => new DTO_GetAll
                        {
                          id = dataRow.Field<int>("id"),
                          FirstName = dataRow.Field<String>("FirstName"),
                            LastName = dataRow.Field<String>("LastName"),
                            Email = dataRow.Field<String>("Email"),
                            Address = dataRow.Field<String>("Address"),
                            Password = dataRow.Field<String>("Password"),
                            UserName = dataRow.Field<String>("UserName"),
                            Role_Id = dataRow.Field<int>("Role_Id"),
                            Status_Id = dataRow.Field<int>("Status_Id")
          }).ToList();

        return result;
      }
   }

Your implementing class can just declare the return type as List<DTO_GetAll> :您的实现类可以将返回类型声明为List<DTO_GetAll>

class SEC_DTO_GetAll : SEC_Interface<DTO_GetAll>
{
    public List<DTO_GetAll> GetAll()
    {

    }
}

You declare that T will be DTO_GetAll when you implement the interface.你在实现接口时声明T将是DTO_GetAll

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

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