简体   繁体   English

可以在扩展方法中使用通用T吗?

[英]Can I use generic T in an extension method?

Trying to mimic this post: Create Generic method constraining T to an Enum 试图模仿这篇文章: 创建将T约束为枚举的泛型方法

public static string[] ToTextArray(this Dictionary<string, T> dictionary) where T:struct, IConvertible
{
    ...
}

It appears my T cannot be resolved. 看来我的T无法解决。 What is the correct way to write an extension method for all Dictionary<string, enum_type> classes? 为所有Dictionary<string, enum_type>类编写扩展方法的正确方法是什么?

You're missing the generic type parameter in the method declaration: 您在方法声明中缺少通用类型参数:

public static string[] ToTextArray<T>(this Dictionary<string, T> dictionary) 
  where T: struct, IConvertible

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

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