C#泛型,方法'xxxxxxxx'的类型参数不能用于使用。 [英] C# generics, the type arguments for method 'xxxxxxxx' cannot be infered from the usage.

查看:101
本文介绍了C#泛型,方法'xxxxxxxx'的类型参数不能用于使用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#泛型问题,



我在来电方面遇到以下错误:



无法根据用法推断方法'xxxxxxxx'的类型参数。尝试明确指定类型参数。



来电者:



C# Generics question,

I'm getting the following error on the caller side:

The type arguments for method 'xxxxxxxx' cannot be inferred from the usage. Try specifying the type arguments explicitly.

The caller:

List<TransferObject> lstTransferObject = new List<TransferObject>();

lstTransferObject = GetDtoList(a, b);





错误在GetDtoList方法上,我尝试将调用指定为.GetDtoList((string)a ,(byte [])b)这不起作用,所以我误解了错误信息。 GetDtoList的来源如下:





The error is on the GetDtoList method, I've tried specifying the call as .GetDtoList((string) a, (byte[]) b) and this didn't work, so I'm misinterpreting the error message. The source for GetDtoList is as follows:

public static List<T> GetDtoList<T>(string k, byte[] s)
{
    List<T> lst = new List<T>();
    foreach(Code_dto dto in Code_GetList())
    {
        dynamic item = new { Name = "code", Data = dto.GetCodeString(k, s) };
        lst.Add(item);
    }

    return lst;
}







回答以下问题......没有什么令人兴奋的,它只返回一个列表。看起来像这样:




In response to question below... nothing exciting, it just returns a list. Looks something like this:

private static List<Code_dto> Code_GetList()
{
	List<Code_dto> lstCodeDto = new List<Code_dto>();
	
	// Grab records from the db and add to the list
	
	return lstCodeDto;
}





我的尝试:



1)其他队友的问题

2)研究类似的堆栈& CP问题(大多数似乎是基于MVC的)。



What I have tried:

1) Ran issue by other teammate
2) Researched similar stack & CP issues (most seem to be MVC based).

推荐答案

泛型类型参数的类型参数只有在输入参数中使用时才能推断。在所有其他情况下,必须明确声明类型参数。



可以推断所有这些的类型参数:

The type argument for a generic type parameter can only be inferred if it is used in an input parameter. In all other cases, the type parameter must be declared explicitly.

The type parameter can be inferred for all of these:
public static T Foo<T>(T bar) { ... }
int x = Foo(42);

public static T FirstOrDefault<T>(IEnumerable<T> source) { ... }
string item = FirstOrDefault(myListOfStrings);

public static IEnumerable<T> Where<T>(IEnumerable<T> source, Func<T, bool> predicate) { ... }
IEnumerable<Bar> filteredList = Where(myListOfBars, bar => someCondition);





但是当没有任何输入参数与type参数相关时,你必须明确指定type参数:



But when none of the input parameters are related to the type parameter, you have to specify the type parameter explicitly:

public static List<T> MakeAList<T>(int x, string y) { ... }

List<Bar> myList = MakeAList(42, "Hello"); // Compiler error
List<Bar> myList = MakeAList<Bar>(42, "Hello"); // OK





因此,在您的示例中,您需要:



So, in your example, you need:

lstTransferObject = GetDtoList<TransferObject>(a, b);





HOWEVER ,这个无效。您的代码将编译,但您将在运行时获得异常,因为您尝试将一种类型的对象添加到不同类型的列表中。如果用对象 T 替换动态键工作,你会得到一个编译错误,它会告诉你问题是什么。



如果你想返回一个动态列表对象,那么你的方法应该返回 List< dynamic> 。如果要返回特定类型的列表,则需要在循环中创建该特定类型的实例。



HOWEVER, this will not work. Your code will compile, but you will get an exception at runtime, because you're trying to add an object of one type to a list of a different type. If you replace the dynamic keywork with either object or T, you'll get a compiler error which shows you what the problem is.

If you want to return a list of dynamic objects, then your method should return List<dynamic>. If you want to return a list of a specific type, then you need to create an instance of that specific type in your loop.


这篇关于C#泛型,方法'xxxxxxxx'的类型参数不能用于使用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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