依赖泛型参数
本文关键字:参数 泛型 依赖 | 更新日期: 2023-09-27 18:36:56
>我有一个Dictionary<Type, Dictionary<Guid,Component>>
,其中外部字典的键是存储在内部对象的类型。
我想使用泛型方法获取内部字典中的一个对象。像这样:
public T getObject<T>(Guid id, ???/*typeof(T) passed here*/) where T : Component
如何将第二个参数约束为 typeof(T) ?
正如@Chirs Pickford所说;没有必要首先接收额外的参数。
当你有这样的泛型方法时:
public T getObject<T>(Guid id) where T : Component
{
// you can grab typeof(T) like this
var type = typeof(T);
}
如果你仍然想这样做,你需要用 T 标记你的类:
public class YourClass<T> where T: Component
{
public T getObject<T>(Guid id, T myvalue) ...