我有一个泛型方法,它有两个通用参数.我试图编译下面的代码,但它不起作用.它是.NET限制吗?是否可以为不同的参数设置多个约束?
public TResponse Call<TResponse, TRequest>(TRequest request)
where TRequest : MyClass, TResponse : MyOtherClass
Run Code Online (Sandbox Code Playgroud) 我的项目中有一个通用存储库.请考虑以下控制器代码段
public class Lookup1Controller : Controller
{
readonly MyDbContext _db;
public Lookup1Controller(MyDbContext dataContext)
{
_db = dataContext;
}
public async Task<IActionResult> Index()
{
IGenericRepository<Lookup1> _repository = new GenericRepository<Lookup1>(_db);
var lookup1s = await _repository.SelectAll();
return View(lookup1s);
}
Run Code Online (Sandbox Code Playgroud)
我认为不需要在我的Generic存储库以及每个控制器中都有我的数据库引用.
我重构它:
public class Lookup1Controller : Controller
{
private IGenericRepository<Lookup1> _repository;
public Lookup1Controller(IGenericRepository<Lookup1> repository)
{
_repository = repository;
}
public async Task<IActionResult> Index()
{
var lookup1s = await _repository.SelectAll();
return View(lookup1s);
}
}
Run Code Online (Sandbox Code Playgroud)
从我读到的内容来看,它更简洁,也是ASP.NET 5的最佳实践.但如果我在浏览器中访问该控制器路径,我将收到以下错误:
InvalidOperationException: Unable to resolve service for type 'MyProject.Data.IGenericRepository`1[MyProject.Models.Lookup1]' while …Run Code Online (Sandbox Code Playgroud) 我正在阅读Anders Hejlsberg等的第4版"C#编程语言"一书.
有几个定义有点扭曲:
未绑定的泛型类型:泛型类型声明本身表示未绑定的泛型类型...
构造类型:包含至少一个类型参数的类型称为构造类型.
open type:open类型是一种涉及类型参数的类型.
闭合类型:闭合类型是不是开放类型的类型.
unbound type:指非泛型类型或非绑定泛型类型.
bound类型:指非泛型类型或构造类型.[annotate] ERIC LIPPERT:是的,非泛型类型被认为是绑定和未绑定的.
问题1,是否低于我列出的正确值?
int //non-generic, closed, unbound & bound,
class A<T, U, V> //generic, open, unbound,
class A<int, U, V> //generic, open, bound, constructed
class A<int, int, V> //generic, open, bound, constructed
class A<int, int, int> //generic, closed, bound, constructed
Run Code Online (Sandbox Code Playgroud)
问题2,书中说"未绑定类型是指由类型声明声明的实体.未绑定泛型类型本身不是类型,它不能用作变量,参数或返回值的类型,或者一个基本类型.唯一可以引用未绑定泛型类型的构造是typeof表达式(第7.6.11节)." 很好,但下面是一个可以编译的小测试程序:
public class A<W, X> { }
// Q2.1: how come unbounded generic type A<W,X> can be used as a …Run Code Online (Sandbox Code Playgroud) .NET泛型术语有点含糊不清.更糟糕的是 - 它似乎在不同来源中含糊不清地使用.基本上不清楚的是这4个术语之间的关系(与"类型"有关):
我明白这List<T>是开放的并且List<int>是封闭的.但是真正与开放/封闭类型相关的"构造"和"未绑定"是什么?
根据stackoverflow的这个答案,C#中的泛型类型在运行时被解析.
但是,根据这个答案,在C#中,泛型类型在编译时被解析.
我在这里错过了什么?
换句话说,是T在编译时还是运行时解析了类型?
更新:
基于Oded的答案,在这种情况下,类型是封闭的具体类型(这意味着它将在编译时解析)
class Program
{
static void Main()
{
var t = new Test<int>();
}
}
public class Test<T>
{
}
Run Code Online (Sandbox Code Playgroud)
将MSIL具有相当于
class Program
{
static void Main()
{
var t = new Test();
}
}
public class Test<int>
{
}
Run Code Online (Sandbox Code Playgroud) class Program
{
static void Main(string[] args)
{
Type t = typeof(A<,>);
Console.WriteLine(typeof(A<,>)); // prints A'2[T1,T2]
}
}
class A<T1,T2>
{
}
Run Code Online (Sandbox Code Playgroud)
据我所知,泛型类型A<T1, T2>不是实际类型,而是实际类型的蓝图/模板,所以为什么typeof接受它作为参数(因为据我所知,typeof接受作为参数实际类型)?
谢谢
我发现了有趣的 C# 库。而且代码中有一个奇怪的语法,我不明白。这样的语法意味着什么:
IRequestHandler<,>
Run Code Online (Sandbox Code Playgroud)
?是一样的IRequestHAndler<T1, T2>还是别的什么?
我有一个泛型类,它包含一个公共属性,它是与父类相同类型的通用接口.示例代码如下.
public interface IExample<T>
{
T Value { get; set; }
string Name { get; set; }
}
public class Example<T> : IExample<T>
{
public string Name { get; set; }
public T Value { get; set; }
}
public class Parent<T>
{
public string ParentName { get; set; }
public IExample<T> ExampleItem { get; set; }
}
public class MainClass
{
public Parent<int> IntParent { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在使用JSON.net来序列化MainClass可以包含许多Parent<T>对象的对象. Parent<T>可以是没有类型约束的任何泛型.但是,我似乎无法以通用方式反序化生成的JSON.
我试图JsonConverter为JSON.net反序列化器创建一个,但我找不到一种通用的方法. …
在下面的示例中,我试图理解为什么BaseType不是泛型类型定义,更一般地说,为什么它不只是等于typeof(List<>)
public class MyList<T> : List<T>
{
}
// this is true by definition
typeof(List<>).IsGenericTypeDefinition
// this is false
// also the BaseType.FullName is null ?!?
// also BaseType.IsConstructedGenericType is true ?!?!?
// as such, BaseType != typeof(List<>)
typeof(MyList<>).BaseType.IsGenericTypeDefinition;
Run Code Online (Sandbox Code Playgroud)
编辑-以下是相关主题的一些有用链接:
Type.IsGenericTypeDefinition 和 Type.ContainsGenericParameters 之间的区别
https://learn.microsoft.com/en-us/dotnet/api/system.type.isgenerictype
c# ×8
generics ×8
.net ×3
.net-3.5 ×1
.net-4.6 ×1
asp.net-core ×1
json ×1
type-systems ×1
types ×1