简体   繁体   English

C#无界泛型类型作为约束

[英]C# unbounded generic type as constrain

Is it possible to have a generic constraint which is an unbounded generic type? 是否可以使用通用约束,这是一种无界泛型类型?

For example: 例如:

public T DoSomething<T>(T dictionary) where T : IDictionary<,>
{
    ...
}

Edit: Just to explain the context, I want to constrain the usage of the method to an IDictionary, but for the method itself it does not matter exactly what TKey and TValue are. 编辑:为了解释上下文,我想将方法​​的用法限制为IDictionary,但对于方法本身而言,TKey和TValue究竟是什么并不重要。

这是不可能的,您需要指定类型参数:

public T DoSomething<T, TKey, TValue>(T dictionary) where T : IDictionary<TKey, TValue> { ... }

There is an issue in rosylin github , however, it's not done yet. rosylin github中存在一个问题,但是,它还没有完成。 So it's not possible right now, but I expect it will be relatively soon. 所以现在不可能,但我预计它会很快。

No, you can't do that. 不,你做不到。 Maybe you can use 也许你可以使用

public IDictionary<TKey, TValue> DoSomething<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
{
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM