不能Implicityly转换类型为void至类型System.Collections.Generic.IList< T> [英] Cannot Implicityly Convert Type Void To Type System.Collections.Generic.IList<T>

查看:339
本文介绍了不能Implicityly转换类型为void至类型System.Collections.Generic.IList< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图返回一个泛型类型,我得到的称号错误描述下。
我相信我在做一些愚蠢 - 建议表示赞赏...

 公共静态的IList< T> GetGroupById< T>(INT的groupId)
{

DashboardGroupType类型=(DashboardGroupType)的groupId;
&IList的LT; T>结果= NULL;

VAR OBJ =默认(T);

开关(类型)
{
情况下DashboardGroupType.Countries:
中断;
情况下DashboardGroupType.Customers:
//此方法返回的IEnumerable典型℃的列表,客户和GT;
OBJ =(T)CustomerRepository.GetAllCustomers();
中断;
情况下DashboardGroupType.Facilities:
//此方法返回的IEnumerable典型℃的列表,设施及GT;
OBJ =(T)FacilityRepository.GetAllFacilities();
中断;
情况下DashboardGroupType.Heiarchy:
中断;
情况下DashboardGroupType.Lines:
中断;
情况下DashboardGroupType.Regions:
//此方法返回的IEnumerable典型℃的名单;串GT;
OBJ =(T)CustomerRepository.GetRegionsHavingCustomers();
中断;
情况下DashboardGroupType.States:
// //此方法返回的IEnumerable典型℃的列表,客户和GT;
OBJ =(T)CustomerRepository.GetStatesHavingCustomers();
中断;
情况下DashboardGroupType.Tanks:
中断;
默认:
中断;
}

结果= result.Add(OBJ); //引发错误的位置

}


解决方案

方法不返回任何东西。它只是改变列表。这就是为什么你得到错误。只是删除分配:

  result.Add(OBJ); 



另一个问题是,你没有初始化的结果。当运行代码时,你会得到一个NullReferenceException。你需要的东西是这样的:

 的IList< T>结果=新的List< T>(); 

您还需要从这个函数返回一个值。我猜你想

 返回结果; 

根据您的意见,这些方法 CustomerRepository.GetAllCustomers(); FacilityRepository.GetAllFacilities(); 和喜欢返回> IEnumerable的LT 的IEnumerable<设施及GT; 或类似。您是铸造这些到T.这意味着所有这些类型都被强制转换为吨。



我猜你想要的就是把所有的项目在这些集合,并将它们添加到列表中。如果是这样的话,你应该转换为的IEnumerable< T> 代替,并调用的 的AddRange 方法。



总体而言,这似乎是一个非常设计不佳。这取决于你想要达到的,它可能会使用继承和/或接口,以获得更好的设计。


Trying to return a generic type and am getting the error decribed in the title. I am sure I am doing something silly - suggestions appreciated...

public static IList<T> GetGroupById<T>(int groupId)
        {

            DashboardGroupType type = (DashboardGroupType)groupId;
            IList<T> result = null;

            var obj = default(T);

            switch (type)
            {
                case DashboardGroupType.Countries:
                    break;
                case DashboardGroupType.Customers:
                    // this returns a list of typ  IEnumerable<Customer>
                    obj = (T) CustomerRepository.GetAllCustomers();
                    break;
                case DashboardGroupType.Facilities:
                    // this returns a list of typ  IEnumerable<Facility>
                    obj = (T) FacilityRepository.GetAllFacilities();
                    break;
                case DashboardGroupType.Heiarchy:
                    break;
                case DashboardGroupType.Lines:
                    break;
                case DashboardGroupType.Regions:
                    // this returns a list of typ  IEnumerable<string>
                    obj = (T) CustomerRepository.GetRegionsHavingCustomers();
                    break;
                case DashboardGroupType.States:
                    // // this returns a list of typ  IEnumerable<Customer>
                    obj = (T) CustomerRepository.GetStatesHavingCustomers();
                    break;
                case DashboardGroupType.Tanks:
                    break;
                default:
                    break;
            }

            result = result.Add(obj); // ERROR IS THROWN HERE

        }

解决方案

The Add method does not return anything. It simply changes the list. That's why you're getting the error. Just remove the assignment:

result.Add(obj);

Another problem is that you're not initializing result. You will get a NullReferenceException when running the code. You need something like this:

IList<T> result = new List<T>();

You'll also need to return a value from this function. I'm guessing you want to

return result;

According to your comments, the methods CustomerRepository.GetAllCustomers(); and FacilityRepository.GetAllFacilities(); and the likes return instances of IEnumerable<Customer>, or IEnumerable<Facility>, or similar. You are casting these to T. This means that all of those types have to be castable to T.

I'm guessing that what you wanted was to take all the items in those collections and add them to the list. If that is the case, you should cast to IEnumerable<T> instead, and call the AddRange method.

Overall, this seems like a very poor design. Depending on what you're trying to achieve, it may be possible to get a better design using inheritance and/or interfaces.

这篇关于不能Implicityly转换类型为void至类型System.Collections.Generic.IList&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆