Determine if object derives from collection type

前端 未结 11 2183
时光说笑
时光说笑 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:50

    I love generics. In this method T must have a public and parameterless constructor which means you can not use IList for T. You must use List

    public static T IsEnumerable() where T : new() {
        if (new T() is IEnumerable) {
    }
    

    提交回复
    热议问题