我正在通过Asp.Net MVC课程并了解到,对于一种方法来确定控制器的动作,
我对某些泛型有所了解并在某种程度上使用它们,但是:
这是方法重载的一个非常基本的示例,两个方法具有相同的名称但具有不同的签名:
int MyMethod(int a)
int MyMethod(int a, string b)
Run Code Online (Sandbox Code Playgroud)
现在假设我定义了两个通用接口,共享完全相同的名称但具有不同数量的类型参数,例如:
IMyInterface<T>
IMyInterface<T1,T2>
Run Code Online (Sandbox Code Playgroud)
我可以说这代表"通用接口重载"吗?或者"重载"术语是否仅适用于此类背景下的方法?它仍然看起来与方法重载非常相似,因为我们保持一个完全相同的名称,但改变参数.
如果我不能说"通用接口过载/重载"我可以说这两个不同的接口共享相同的名称?
感谢和抱歉,如果这是一个愚蠢的问题,但谷歌搜索"通用接口重载"或"通用接口重载"并没有给我很多,但有关接口方法重载的结果,这不是我感兴趣的.
实例属性的文档Type.IsConstructedGenericType不清楚或具有误导性.
我尝试了以下代码来查找此属性和相关属性的实际行为:
// create list of types to use later in a Dictionary<,>
var li = new List<Type>();
// two concrete types:
li.Add(typeof(int));
li.Add(typeof(string));
// the two type parameters from Dictionary<,>
li.Add(typeof(Dictionary<,>).GetGenericArguments()[0]);
li.Add(typeof(Dictionary<,>).GetGenericArguments()[1]);
// two unrelated type parameters
li.Add(typeof(Func<,,,>).GetGenericArguments()[1]);
li.Add(typeof(EventHandler<>).GetGenericArguments()[0]);
// run through all possibilities
foreach (var first in li)
{
foreach (var second in li)
{
var t = typeof(Dictionary<,>).MakeGenericType(first, second);
Console.WriteLine(t);
Console.WriteLine(t.IsGenericTypeDefinition);
Console.WriteLine(t.IsConstructedGenericType);
Console.WriteLine(t.ContainsGenericParameters);
}
}
Run Code Online (Sandbox Code Playgroud)
代码贯穿由36种类型组成的笛卡尔积t.
结果:对于32种类型(所有,但4个组合Dictionary<int, int>,Dictionary<int, string>, …