Convert / Cast IEnumerable to IEnumerable

后端 未结 4 1156
甜味超标
甜味超标 2020-12-03 00:41

I have a class (A web control) that has a property of type IEnumerable and would like to work with the parameter using LINQ.

Is there any way to cast / convert / inv

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 01:00

    This is years later, but I solved the List problem.

    void Method(IEnumerable source)
    {
        var enumerator = source.GetEnumerator();
        if (enumerator.MoveNext())
        {
            MethodInfo method = typeof(MyClass).GetMethod("Method2");
            MethodInfo generic;
            Type type = enumerator.Current.GetType();
            bool sameType = true;
    
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.GetType() != type)
                {
                    sameType = false;
                    break;
                }
            }
    
            if (sameType)
                generic = method.MakeGenericMethod(type);
            else
                generic = method.MakeGenericMethod(typeof(object));
    
            generic.Invoke(this, new object[] { source });
        }
    }
    

    提交回复
    热议问题