【问题标题】:Hiding extension methods for certain generic types in IntelliSense在 IntelliSense 中隐藏某些泛型类型的扩展方法
【发布时间】:2013-02-11 13:14:58
【问题描述】:

我有一个扩展方法:

public static void Foo<T>(this MyClass<T> target)
    where T : IEnumerable
{
    // Code goes here
}

如果 T 是一个字符串,我想从 InteliSense 隐藏这个扩展方法,而不是其他类型,就像在处理字符串时隐藏 Linq 中 IEnumerable 的扩展方法一样。 我查看了EditorBrowsableAttribute,但它似乎不允许隐藏基于泛型类型的方法。

【问题讨论】:

    标签: c# extension-methods intellisense


    【解决方案1】:

    尝试将EditorBrowsableAttribute 放置到此方法(也可能放置到包含此方法的类中)并将该方法重新定位到另一个程序集(另一个项目)。之后,编译程序集并卸载项目,因此您将只有 DLL 引用。这是使用EditorBrowsableAttribute力量的唯一方法。

    【讨论】:

    • 我希望该方法在 IntelliSense 中可见,我只是希望它在 Tstring EditorBrowsableAttribute 时对所有方法不可见。
    • @Cornelius 这在语义上是不可能的,因为string 清楚明确地实现了IEnumerableIEnumerable&lt;char&gt;。你对此无能为力。
    • @Cornelius 您唯一可以尝试的是创建重载,以接受您需要的所有类型,除了char 作为TIEnumerable
    猜你喜欢
    • 2011-11-15
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多