Determine if object derives from collection type

前端 未结 11 2049
时光说笑
时光说笑 2021-02-05 04:53

I want to determine if a generic object type (\"T\") method type parameter is a collection type. I would typically be sending T through as a Generic.List but it could be any co

11条回答
  •  无人及你
    2021-02-05 05:36

    Also, remember just because you are using generics, don't forget other basic techniques, in this case, like overloading. I suspect the you are planning something like this:

    void SomeFunc(T t)
    {
        if (IsCollectionCase(t))
           DoSomethingForCollections()
        else
           DoSOmethingElse();
    }
    

    This would be far better handled as:

    void SomeFunc(IEnumerable t)
    {
           DoSomethingForCollections()
    }
    void SomeFunc(T t)
    {
           DoSomethingElse()
    }
    

提交回复
热议问题