如何定义具有两个泛型类型和两个类型约束的方法

本文关键字:两个 类型 约束 方法 泛型类型 何定义 定义 | 更新日期: 2023-09-27 18:20:19

我的方法定义为

public ActionResult _Create3<T>() where T:IMyClass, new()

但我想定义两个通用类型

public ActionResult _Create3<T, G>(G content) where T:IMyClass, new()

类型G也必须使用接口ImyClass,但我不知道在哪里定义拖类型!!!

例如,如果可以写入:

public ActionResult _Create3<T, G>(G content) where {T:IMyClass, G:IMyClass}, new()

但是出错了。

感谢回答

如何定义具有两个泛型类型和两个类型约束的方法

为该泛型类型添加另一个where约束:

public ActionResult _Create3<T, G>(G content) 
  where T : IMyClass, new()
  where G : IMyClass, new()

您可以在多个泛型类型上定义多个where约束,如:

public ActionResult _Create3<T, G>(G content) where T:IMyClass, new()
                                              where G:IMyClass, new()