为什么泛型类型上的静态方法需要Type参数? [英] Why does a static method on a generic type require a Type parameter?

查看:107
本文介绍了为什么泛型类型上的静态方法需要Type参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class BinarySearchTree<T>
where T : IComparable<T>
{
    public static BinarySearchTree<char> InitializeSampleCharacterBST()
    {
        var bst = new BinarySearchTree<char>();

        bst.Insert('F');
        bst.Insert('B');
        bst.Insert('A');
        bst.Insert('D');
        bst.Insert('C');
        bst.Insert('G');
        bst.Insert('I');
        bst.Insert('H');

        return bst;
    }

class Program
{
        static void Main(string[] args)
        {
            var bst = BinarySearchTree.InitializeSampleCharacterBST();
        }
}

为什么这是非法的?期望我为没有意义的类的方法调用提供类型参数.泛型类或方法在静态上下文中没有任何类型参数.

Why is this illegal? It's expecting me to provide a type parameter to the method call for the class which makes no sense. A generic class or method has no use for a type parameter in a static context.

它要我这样写电话:

var bst = BinarySearchTree<foo>.InitializeSampleCharacterBST();

在哪里foo可以是我想要的任何类型,而不管静态方法调用返回特定类型的泛型对象的事实.

Where foo can be any type I want regardless of the fact that the static method call returns a specifically typed generic object.

推荐答案

正如Marc所说,有时将类型重载为具有非泛型类很有用-在这种情况下就是这种情况.

As Marc said, it's sometimes useful to overload the type to have a non-generic class - and it would be in this case.

关于为什么有必要,假设静态方法实际上是通过以下方式实现的:

As for why it's necessary, suppose that the static method were actually implemented as:

public static BinarySearchTree<char> InitializeSampleCharacterBST()
{
    Console.WriteLine(typeof(T));
    return null;
}

那将是完全有效的代码-它是泛型类型,因此它应该可以访问类型参数...但是您试图在不提供泛型类型参数的情况下调用该方法,因此不可能工作.在您的情况下,您碰巧不在该方法中的任何地方使用T,但这是一个巧合.就像有一个不使用this的实例方法:您没有使用该实例,但是仍然无法像调用静态方法一样调用它.

That would be perfectly valid code - it's in a generic type, so it should have access to the type parameter... but you're trying to call the method without providing a generic type parameter, so it couldn't possibly work. In your case you happen to not use T anywhere within the method, but that's a coincidence. It's a bit like having an instance method which doesn't use this: you're not using the instance, but you still can't call it as if it were a static method.

除了具有单独的静态类之外,另一种有用的设计技术是将您的类型分为非通用和通用部分.这样,在弄清楚您拥有哪种确切类型的情况下,您实际上不需要知道它即可调用某些成员.例如,收集接口层次结构可能具有:

As well as having separate static classes, another design technique which can be useful is to split your type into non-generic and generic pieces. That way, in cases where it can be awkward to work out which exact type you have, you don't actually need to know it in order to call some of the members. For example, a collection interface hierarchy might have:

public interface ISomeCollection
{
    int Count { get; }
    void Clear();
}

public interface ISomeCollection<T> : ISomeCollection
{
    void Add(T item);
}

我本人已将此技术用于C#的Protocol Buffers端口,事实证明它非常有用(如果有些复杂的话).

I've used this technique myself for my Protocol Buffers port to C#, and it's proved very useful (if somewhat complicated).

这篇关于为什么泛型类型上的静态方法需要Type参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆